Detailed description of layoutinflater. Inflate

Source: Internet
Author: User
Tags xml parser
Common inflate Methods

In daily development, we often use resource IDs to obtain views. There are four methods to obtain views:

// 1. Obtain layoutinflater Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service); view = Inflater. inflate (resource, root, attachtoroot); // 2. Use the getlayoutinflater () method in the activity to view = getlayoutinflater (). inflate (resource, root, attachtoroot); // 3. Use the static inflate () method of view to view = view. inflate (resource, root, attachtoroot); // 4, view = layoutinflater through the inflate () method of layoutinflater. from (this ). inflate (resource, root, attachtoroot );


Through the source code analysis of the above methods, it is easy to see that these methods are ultimately called method 1, the way to obtain the system layout loader, to obtain 'view '.


The 'inflate (INT resource, viewgroup root) 'overload method is not listed here, because they will eventually call 'inflate (INT resource, viewgroup root, Boolean attachtoroot) 'method, as follows:

 public View inflate(int resource, ViewGroup root) {        return inflate(resource, root, root != null); }  public View inflate(int resource, ViewGroup root, boolean attachToRoot) {        if (DEBUG) System.out.println("INFLATING from resource: " + resource);        XmlResourceParser parser = getContext().getResources().getLayout(resource);        try {            return inflate(parser, root, attachToRoot);        } finally {            parser.close();        } }

It should be noted that, in fact, the final call is the 'inflate (xmlpullparser parser, viewgroup root, Boolean attachtoroot) 'method, but the layout resource is parsed as the xmlpull parser here, here we will not study 'xmlpullparser.


Analyze different input parameters by interpreting the source code of the 'inflate (xmlpullparser parser, viewgroup root, Boolean attachtoroot) 'method, 'xmlpullparser parser 'is an XML parser that imports 'int resource'. If you do not need to consider it, you need to consider that the values of 'viewgroup root and Boolean attachtoroot' are different, what will happen?


The source code contains several important code blocks:

If (root! = NULL) {If (Debug) {system. out. println ("creating Params from root:" + root);} // if the root value is not null, obtain its layoutparams Params = root. generatelayoutparams (attrs); If (! Attachtoroot) {// attachtoroot equals false, and the layoutparams attribute of root is assigned to temptemp. setlayoutparams (Params );}}

// Attachtoroot equals to true. Add temp to the root viewgroup if (root! = NULL & attachtoroot) {root. addview (temp, Params);} // root equals null, attachtoroot equals false, and temp is directly assigned to the returned result if (root = NULL |! Attachtoroot) {result = temp ;}

Implement Based on Different values

Two layout files, one as root and the other as the view to be retrieved


Activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MyActivity">    <TextView        android:text="@string/hello_world"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>

View. xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="120dp"    android:layout_height="120dp"    android:background="@color/blue">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="hello inflate"        android:textColor="@android:color/white" /></LinearLayout>




1. rootview equals null, attachtoroot equals false

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);    view = getLayoutInflater().inflate(R.layout.view, null, false);    setContentView(view);}



If the root value is null, The layoutparams of the view will not be obtained. The result is returned using 'result = temp; ', and the result is equal to the view


Why is the screen filled? Because when the view is set to the view of the activity, the layoutparm of the current window is used as the layoutparm OF THE VIEW




2. The value of rootview is null, and the value of attachtoroot is true.

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);    view = getLayoutInflater().inflate(R.layout.view, null, true);    setContentView(view);}


Root is equal to null and still follows 'result = temp; '. The result is equal to view


Why is the screen filled? Because when the view is set to the view of the activity, the layoutparm of the current window is used as the layoutparm OF THE VIEW


3. The value of rootview is not null, and the value of attachtoroot is false.

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);rootView = getLayoutInflater().inflate(R.layout.activity_my, null);    view = getLayoutInflater().inflate(R.layout.view,(ViewGroup)rootView,false);    setContentView(view);}


Root is not equal to null, attachtoroot is equal to false, the layoutparams of the view will be taken and assigned to temp, and then 'result = temp; ', then the result is equal to view


Why is the screen filled? Because when the view is set to the view of the activity, the layoutparm of the current window is used as the layoutparm OF THE VIEW


4. The value of rootview is not null, and the value of attachtoroot is true.
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);rootView = getLayoutInflater().inflate(R.layout.activity_my, null);    view = getLayoutInflater().inflate(R.layout.view,(ViewGroup)rootView,true);    setContentView(view);}

Rootview is not equal to null, attachtoroot is equal to true, the view Params will be obtained first, and then 'root. addview (temp, Params) ', that is, put the view in the root viewgroup first, and then return the result, because the result is equal to root during initialization, the returned result is the root with a sub-view.


Here, because the root layout is relativelayout, we add the view to the root, and the view itself retains its own layoutparm.


Finally, I want to talk about why csdn does not support markdown. I wrote it first with MOU, and I had to re-Print it myself...

Detailed description of layoutinflater. Inflate

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.