The inflate method of Android programming Layoutinflater

Source: Internet
Author: User

The inflate method of layoutinflater is often used in the Oncreateview method of fragment:

[Java]View Plaincopy
    1. Public View Oncreateview (layoutinflater inflater, ViewGroup container,
    2. Bundle savedinstancestate) {


There are four kinds of inflate methods in Layoutinflater, but these are the only two that we use frequently in our daily use:

[Java]View Plaincopy
    1. Public View Inflate (int resource, ViewGroup root) {
    2. return Inflate (resource, root, root! = null);
    3. }

[Java]View Plaincopy
  1. Public View Inflate (int resource, ViewGroup root, boolean attachtoroot) {
  2. if (DEBUG) System.out.println ("inflating from Resource:" + Resource);
  3. Xmlresourceparser parser = GetContext (). Getresources (). GetLayout (Resource);
  4. try {
  5. return Inflate (parser, Root, attachtoroot);
  6. } finally {
  7. Parser.close ();
  8. }
  9. }

So, here is a direct introduction to this method can be called:

[Java]View Plaincopy
    1. Public View Inflate (Xmlpullparser parser, ViewGroup root, boolean attachtoroot)


In this method, the upper part of the XML parsing code, which is not posted here, there is really nothing to see. Look directly at the middle part of the code:

[Java]View Plaincopy
  1. Viewgroup.layoutparams params = null;
  2. if (root = null) {
  3. if (DEBUG) {
  4. System.out.println ("Creating params from root:" +
  5. root);
  6. }
  7. //Create layout params that match root, if supplied
  8. params = Root.generatelayoutparams (attrs);
  9. if (!attachtoroot) {
  10. //Set the layout params for temp If we is not
  11. //attaching. (If We is, we use AddView, below)
  12. Temp.setlayoutparams (params);
  13. }
  14. }

params = Root.generatelayoutparams (attrs);

This paragraph means: If the inflate method is called and the ViewGroup root parameter is passed in, the layoutparams of Layout_width and Layout_height will be obtained from Root. If the attachtoroot is set to False, the Layoutparams will be set for the view that we loaded.

Then look down:

[Java]View Plaincopy
  1. We is supposed to attach all the views we found (int temp)
  2. to root. Do it now.
  3. if (root = null && attachtoroot) {
  4. Root.addview (temp, params);
  5. }
  6. Decide whether to return the root is passed in or the
  7. Top view found in XML.
  8. if (root = Null | |!attachtoroot) {
  9. result = temp;
  10. }

Root.addview (temp, params);

If the ViewGroup root parameter is set and Attachtoroot is set to True, the view we load is added to the root view as a child view.

If we viewgroup root is set to NULL, we return directly to the view we created, and if root is not empty and Attachtoroot is set to false, return to the above paragraph: set the Layoutparams for the view we loaded.

The above is the method content, you may still have a little to see, the next article, I will do a few examples, to specify.

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:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="50DP"
  9. android:gravity="center"
  10. android:text="Hello World" />
  11. </linearlayout>


The Test.xml file is:

[HTML]View Plaincopy
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="200DP"
  5. android:background="#ffffff00"
  6. android:orientation="vertical" >
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="50DP"
  10. android:gravity="center"
  11. android:text="test" />
  12. </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)

[Java]View Plaincopy
    1. @Override
    2. protected void OnCreate (Bundle savedinstancestate) {
    3. super.oncreate (savedinstancestate);
    4. View view = (LinearLayout) getlayoutinflater (). Inflate (R.layout.main,
    5. null);
    6. view = Getlayoutinflater (). Inflate (r.layout.test, null);
    7. Setcontentview (view);
    8. }

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)

[HTML]View Plaincopy
    1. @Override   
    2. protected void oncreate ( bundle savedinstancestate)  {  
    3.     super.oncreate ( savedinstancestate);   
    4.     view view =   (LinearLayout)  getlayoutinflater (). Inflate (r.layout.main,  
    5.             null);   
    6.    
    7.     view = getlayoutinflater (). Inflate (r.layout.test,  (viewgroup)  view, false);   
    8.   
    9.     setcontentview (view);   
    10. }  


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)

[HTML]View Plaincopy
  1. @Override
  2. protected void OnCreate (Bundle savedinstancestate) {
  3. Super.oncreate (savedinstancestate);
  4. View view = (linearlayout) getlayoutinflater (). Inflate (R.layout.main,
  5. NULL);
  6. view = getlayoutinflater (). Inflate (R.layout.test, (viewgroup) view,
  7. true);
  8. Setcontentview (view);
  9. }

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

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.

The inflate method of 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.