I want to know how the LayoutParams will work on LayoutInflator. And what is difference between:
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //FIRST WAY
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, container,false); //SECOND WAY
Because, both methods gives me different result.
Actually second inflate method are gives me correct result for both child layout change, but First method will gives me different result.
Here is my code:
MainActivity.Java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout=(LinearLayout)findViewById(R.id.mainLayout);
LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
for(int i=0;i<10;i++){
LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, null); //First WAY
// LinearLayout childLayout=(LinearLayout)inflater.inflate(R.layout.childitemlayout, mainLayout,false); //SECOND WAY
mainLayout.addView(childLayout);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
childitemlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical"
android:background="#525f67">
<TextView android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button"
android:gravity="center"
/>
</LinearLayout> <!-- Both ways gives different result -->
<!--
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#525f67">
<TextView android:id="@+id/btn"
android:layout_width="80dp"
android:layout_height="80dp"
android:text="Button"
android:gravity="center"
/>
</LinearLayout> Both method gives SAME result -->
The main difference between the two
inflate()methods is the second parameter(theViewGroupparameter) and its use in setting the properLayoutParamsfor the root view of the inflated layout file. This is important because theLayoutParamskeep various layout attributes of the view(like width, height, positioning rules etc) and are required so the parent of that view can properly show the view.The first method basically says: build the hierarchy view from this layout file but don’t assign
LayoutParamsto the root of the inflated hierarchy(maybe because the parent isn’t know yet), also don’t attach the inflated view to a parent.The second inflate method says: build the hierarchy view from this layout file and also assign the proper
LayoutParams(based on the second parameter given to the inflate method) to the root of the inflated hierarchy, also don’t attach the inflated view to a parent.In the first case, the root of the inflated layout file(
R.layout.childitemlayout) will not have anyLayoutParamsset on it(theinflatemethod didn’t assign any because the second parameter isnulland it doesn’t know which type ofLayoutParamsto generate), so your fixed width/height values are lost. Later when you’ll domainLayout.addView(childLayout);themainLayoutwill check theLayoutParamsof thechildLayout, see that those arenulland will automatically set an instance of theLayoutParams(using itsgenerateDefaultLayoutParams()method). This method, in the particular case of a horizontalLinearLayout, will return an instance ofLayoutParamswhere the width/height will be set toWRAP_CONTENT. So yourchildLayoutwill end up withWRAP_CONTENTas its size instead of the fixed values you set on them.In the second case, the
inflatemethod sees that you suggested theLinearLayoutmainLayoutas theViewGroupused for generating theLayoutParams. This means that the fixed values(that you used for the width/height) retrieved from the layout file can be stored in a proper instance of theLayoutParams. When you’ll domainLayout.addView(childLayout);,mainLayoutwill see thatchildLayouthas the properLayoutParamsinstance(which has the values used in the layout file) and doesn’t call itsgenerateDefaultLayoutParams().