Examples of inflate methods for Android programming Layoutinflater

Source: Internet
Author: User

If you don't care about its internal implementation, just look at how to use it, just look at this article.


Next, use the simplest examples to illustrate:

With two layout files, Main and test:

Among them, 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 execution is as follows:


This is very easy to understand. Because I didn't specify ViewGroup root, so. Equivalent to loading the test view file directly. and returns.

And its height is full of full screen instead of 200DP. Because there is no root parameter when executing inflate, you cannot set the Layoutparam parameter for the test view. So why is it filled with screens instead of being displayed? Because 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, the final execution is the same.


Another 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);    }

This is the time to call inflate. Strong turn view for ViewGroup, because it is linearlayout, so here can be strong.

The effect of the execution is as follows:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehl6x2zseq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "height=" "width=" >

Look at the effect, just like the one above.

But from the code itself, the content of the implementation is different.

Because of the viewgroup, the view obtained here has actually been layoutparam. You can print log to see it yourself.

But why the final result is the same as above. The reason is because when you set the view to activity, you get 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 execution is as follows:

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


The sample is very easy and does not include code project.


Assuming that the inflate method is implemented, interested in, can participate in an article:

The inflate method of the layoutinflater of Android programming is explained in detail


Add: The new API will be in Inflater.inflate (r.layout.xxx, NULL);



Please see below for a copy of the reproduced article explaining:
Layout inflation is the term used within the context of Android to indicate when a XML layout resource is parsed and conv Erted into a hierarchy of View objects.


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.