[Android] The LayoutInflater. inflate method and the layout parameters of xml root elements do not work. androidinflate
I. First, check the inflate method with three parameters:
Public View inflate (int resource, ViewGroup root, boolean attachToRoot)
1. If root is not null and attachToRoot is TRUE, a layer of root layout will be nested at the outermost layer of the loaded layout file. In this case, the layout parameter of the xml root element will certainly take effect.
2. If root is not null and attachToRoot is false, a root layout is not nested at the outermost layer of the loaded layout file, this root will only be used to generate layout parameters for the root view of the xml to be loaded (official statement: If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML .),
At this time, the layout parameter of the xml root element also takes effect.!!!
3. If the root value is null, it makes no sense to attachToRoot whether it is true or false! That is, the layout parameter of the xml root element still does not work!
2. Let's look at the inflate method with two parameters:
Public View inflate (int resource, ViewGroup root)
View Source Code:
public View inflate(int resource, ViewGroup root) { return inflate(resource, root, root != null); }
That is to say
1. When root is not null, it is equivalent to 2nd cases of the inflate method with three parameters above.
2. When root is null, it is equivalent to 3rd cases of the inflate method with three parameters above.
Iii. Practice ----- use listview to verify the above theory
You must have encountered the problem that the height set in the item layout of ListView is ineffective.
Item_lv_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dip" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="test" /></LinearLayout>
The getView method of the adapter:
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, null); } return convertView;}
If you use the above Code, you will find that setting 100dp is invalid. If you replace it with the following code, you can.
public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflate(R.layout.item_lv_test, parent, false); } return convertView;}
Here you should think about why the ViewGroup parameter is included in many methods that need to display the View.
Therefore, some people will say that the settings in the layout are invalid, and a layer of layout needs to be nested. This is incorrect, resulting in an increase in layout levels and performance impact.
Reference: http://blog.csdn.net/guolin_blog/article/details/12921889
Https://github.com/CharonChui/AndroidNote/blob/master/Android%E5%AD%A6%E4%B9%A0%E5%8A%A0%E5%BC%BA/LayoutInflater.inflate%E8%AF%A6%E8%A7%A3.md