How Android gets the LayoutInflater object

Source: Internet
Author: User

When writing Android programs, you may sometimes write custom views and use the Inflater object to parse the layout file into a View. This article aims to summarize the methods for obtaining LayoutInflater objects.


1. If you can obtain the context object, you can use the following 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);
 

2. In an Activity, you can use 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 actually use context (). getSystemService ().


3. static View method:

View view=View.inflate(context, R.layout.child, null)

The source code of inflate is 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 inflation.     *     * @param context The Context object for your activity or application.     * @param resource The resource ID to inflate     * @param root A view group that will be the parent.  Used to properly inflate the     * layout_* parameters.     * @see LayoutInflater     */    public static View inflate(Context context, int resource, ViewGroup root) {        LayoutInflater factory = LayoutInflater.from(context);        return factory.inflate(resource, root);    }
LayoutInflater. from (context) is actually packaging method 1. You 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.