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:

    Public View Oncreateview (layoutinflater inflater, ViewGroup container,            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:

    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 fr Om resource: "+ Resource);        Xmlresourceparser parser = GetContext (). Getresources (). GetLayout (Resource);        try {            return inflate (parser, Root, attachtoroot);        } finally {            parser.close ()}    }


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

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:

                    Viewgroup.layoutparams params = null;                    if (root = null) {                        if (DEBUG) {                            System.out.println ("Creating params from root:" +                                    root);                        }                        Create layout params that match root, if supplied                        params = root.generatelayoutparams (attrs);                        if (!attachtoroot) {                            //Set the layout params for temp If we is not                            //attaching. (If We is, we use AddView, below)                            Temp.setlayoutparams (params);                        }                    }


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:

                    We is supposed to attach all the views we found (Int. temp)                    //to root. Do it now.                    if (root = null && attachtoroot) {                        Root.addview (temp, params);                    }                    Decide whether to return the root is passed in or the                    //top view found in XML.                    if (root = null | |!attachtoroot) {                        result = temp;                    }


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.


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.