An explanation of the "Android" Layoutinflater.inflate method and the problem of the layout parameters of the XML root element do not work

Source: Internet
Author: User

first, look at 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, then a root layout is nested at the outermost layer of the loaded layout file, and the layout parameters of the XML root element will of course work.
2. If root is not null and Attachtoroot is false, then a root layout is not nested at the outermost layer of the loaded layout file, which will only be used to generate layout parameters for the root view of the XML to be loaded (official words: If false, Root is only used to create the correct subclass of Layoutparams for the root view in the XML.),
the layout parameters of the XML root element will also work !!!
3, if root is null, then attachtoroot whether true or false is meaningless! That is, the layout parameters of the XML root element still do not work!


second, see the inflate method with two parameters:
Public View Inflate (int resource, ViewGroup root)

View Source:

    Public View Inflate (int resource, ViewGroup root) {        return inflate (resource, root, root! = null);    }
Other words
1, when Root is not NULL, the equivalent of the above with three parameters of the inflate method of the 2nd case
2, when the root is null, the equivalent of the above with three parameters of the inflate method of the 3rd case


third, the actual combat ————— to the ListView to verify the above theory
You must have encountered a problem with no effect on the height set in the ListView item layout.
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>

Adapter Method of GetView:

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 not valid. And if you replace it with the code below.

Public View GetView (int position, View Convertview, ViewGroup parent) {    if (Convertview = = null) {        Convertview = i Nflate (R.layout.item_lv_test, parent, false);    }    return Convertview;}

Here you should think about why a lot of the methods that need to display the view have viewgroup this parameter.
So some people would say that the settings in the layout are not valid, and you want to nest a layer of layout again. This is wrong, resulting in increased layout levels, impacting performance



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



An explanation of the "Android" Layoutinflater.inflate method and the problem of the layout parameters of the XML root element do not work

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.