Overview of how Android gets Layoutinflater objects

Source: Internet
Author: User

When writing an Android program, sometimes a custom view is written, using the Inflater object to parse the layout file into a view. The main purpose of this paper is to summarize the methods of obtaining Layoutinflater objects.


1, if you can get the context object, there are several methods:

Layoutinflater Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); View child = inflater.inflate (R.layout.child, NULL);

Or

Layoutinflater Inflater = layoutinflater.from (context); View child = inflater.inflate (R.layout.child, NULL);
</pre><p></p><pre>
2, in an activity, you can have the following methods:

View child = Getlayoutinflater (). Inflate (R.layout.child, item, FALSE);
Or

View view; Layoutinflater Inflater = (layoutinflater)   getcontext (). Getsystemservice (Context.layout_inflater_service); View = Inflater.inflate (r.layout.mylayout, NULL);

Both Method 1 and Method 2 are actually used for the context (). Getsystemservice ().


3. Static method of using view:

View view=view.inflate (context, r.layout.child, NULL)

Inflate realize the source code as follows:

    /**     * Inflate a view from an XML resource.  This convenience method wraps the {@link     * Layoutinflater} class, which provides a full range of options for view INFL ation.     *     * @param context the context object for your activity or application.     * @param resource The resource ID to inflate     * @param root A View group that would be the parent.  Used to properly inflate the     * layout_* parameters.     * @see layoutinflater     *    /public static View inflate (context context, int resource, ViewGroup root) {        layou Tinflater factory = Layoutinflater.from (context);        Return Factory.inflate (resource, root);    }
Layoutinflater.from (context) is actually the packaging of Method 1, can refer to the following source code:

    /**     * Obtains the layoutinflater from the given context.     */public    static Layoutinflater from (context context) {        Layoutinflater layoutinflater =                (layoutinflater) Context.getsystemservice (context.layout_inflater_service);        if (Layoutinflater = = null) {            throw new Assertionerror ("Layoutinflater not Found.")        ;        return layoutinflater;    }




Related Article

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.