Use of Layoutinflater in Android

Source: Internet
Author: User

First, the role of Layoutinflater

We generally use layoutinflater to do one thing: View inflate (int resource, viewgroup root);

The function of inflate () is similar to that of Findviewbyid (); The difference is that Findviewbyid is used to find specific widget controls (such as Textview,button waits) under a specific XML, and inflate is used to find and instantiate an XML layout file under the/res/layout/folder, with Setco Ntentview () are also different.

Second, how to get Layoutinflater object
    1. static method via Layoutinflater from ()
      Layoutinflater inflater = Layoutinflater.from (this); View Viewnull);
    2. Get through the service
      Layoutinflater inflater = (layoutinflater) Context.getsystemservice (context.layout_inflater_service);
    3. Through the Activity's getlayoutinflater () method
      Layoutinflater inflater = Getlayoutinflater ();

Third, inflate and findbyid attention to detail

Although layout is also a subclass of view, in Android, if you want to convert the layout of XML into view into a. Java code, you can only pass inflater, not through Findviewbyid ().

Iv. The difference between inflate and Setcontentview
    • Inflate common method forms are View inflate (int resource, viewgroup root); Resource is the ID constant of the layout file in the R.java file, this value must specify that root is nullable, empty when only the current layout file is instantiated, and when Root is not empty, automatically adds the currently instantiated layout file object to the child of root.
    • The method form for Setcontentview is the inflater parameter of the void Setcontentview (int layoutresid) Layoutresid with the resource method.

The difference between the two is that once the setcontentview is called, the UI is displayed immediately, and inflate only forms an object that is implemented in the view class, and is then displayed with the Setcontentview (view) when needed. The interface is typically displayed through Setcontentview in the activity, but it is layoutinflater dynamically loaded if the control layout is set to operate on non-activity.

Use of Layoutinflater in Android

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.