Examples of inflate methods for Android programming Layoutinflater

Source: Internet
Author: User

If you do not care about its internal implementation, just look at how to use, directly read this article.


Next, use the simplest examples to illustrate:

With two layout files, Main and test:

Where the Main.xml file is:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <textview        android:layout_width= "match_parent"        android:layout_height= " 50DP "        android:gravity=" center "        android:text=" Hello World "/></linearlayout>

the Test.xml file is:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" 200DP "    android:background=" # Ffffff00 "    android:orientation=" vertical ">    <textview        android:layout_width=" Match_parent        " android:layout_height= "50DP"        android:gravity= "center"        android:text= "test"/></linearlayout>

Set its height to 200DP in test, and set the background color.


Next look at Layoutinflater (). Inflate method Implementation:

First way: Inflate (view, NULL)

    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        View view = (LinearLayout) getlayoutinflater (). Inflate (R.layout.main,                null);        view = Getlayoutinflater (). Inflate (r.layout.test, null);        Setcontentview (view);    }

The effect of the operation is as follows:


This is easy to understand because I did not specify the ViewGroup root parameter, so it is equivalent to loading the test view file directly and returning.

And its height is full of full screen instead of 200DP, because when executing inflate, there is no root parameter, you cannot set the Layoutparam parameter for the test view. So why is it filled with screens instead of being displayed? Because when we set the view to activity, we get the current window's Layoutparam assignment to it, which is full screen. If you are interested, you can change the layout_width of test to set a value, and the result is the same as the last run.


Second way: Inflate (view, root, False)

    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        View view = (LinearLayout) getlayoutinflater (). Inflate (R.layout.main,                null);        view = Getlayoutinflater (). Inflate (R.layout.test, (ViewGroup) view, false);        Setcontentview (view);    }

When calling inflate here, the strong turn of view is ViewGroup, because it is linearlayout, so it can be strong.

The effect of the operation is as follows:

Look at the effect, just like the one above. But from the code itself, the content of the implementation is different. With ViewGroup, the view you get here is already layoutparam, so you can print the log yourself.

But why the final result is the same as above. The reason is that when the view is set to activity, it gets the current window's Layoutparam assignment to it, which is full screen.


Third Way: Inflate (view, root, True)

    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        View view = (LinearLayout) getlayoutinflater (). Inflate (R.layout.main,                null);        view = Getlayoutinflater (). Inflate (R.layout.test, (viewgroup) view,                true);        Setcontentview (view);    }
The effect of the operation is as follows:

The effect is obvious because main is a linear layout, so the test view is added under TextView (Hello World) and retains its own layoutparam parameter.


The example is simple and does not include code engineering.


If you are interested in how the inflate method is implemented, you can refer to the previous article:

The inflate method of Android programming Layoutinflater

Examples of inflate methods for Android programming Layoutinflater

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.