Layoutinflater and inflate in Android

Source: Internet
Author: User

Reprinted: http://hi.baidu.com/aspxdiyer/item/5879980402843519eafe388f

1. layoutinflater

Layoutinflater finds the XML layout file under Res/layout/and instantiates it. This is similar to findviewbyid, the latter is to find the specific widget controls (such as button and textview) in the XML layout file)

Purpose:

1. layoutinflater. Inflate () is required for loading an interface that is not loaded or that is to be dynamically loaded;

2. For a loaded interface, you can use the activiyt. findviewbyid () method to obtain the interface elements.

Layoutinflater is an abstract class, which is declared as follows in the document:

Public abstract class layoutinflater extends object

Three methods for obtaining a layoutinflater instance

1. layoutinflater Inflater = getlayoutinflater (); // call the getlayoutinflater () of the activity ()

Example: View toastroot = getlayoutinflater (). Inflate (R. layout. Toast, null );

2. layoutinflater localinflater = (layoutinflater) Context. getsystemservice (context. layout_inflater_service );

3. layoutinflater Inflater = layoutinflater. From (context );

Example: View convertview = layoutinflater. From (mcontext). Inflate (R. layout. activity_contact, null );

2. Inflate

In general, inflate is equivalent to finding the layout defined in an XML file. if findviewbyid () is used directly in an activity, it corresponds to the component in the layout of setconentview.

Therefore, if you use another layout in your activity, such as layout in the dialog box, you must set the content of components (such as image imageview and text textview) in layout in the dialog box, you must use inflate () to first find the layout in the dialog box, and then use this layout object to find the components above it, such:

  

View view = view. Inflate (this, R. layout. dialog_layout, null );

Textview dialogtv = (textview) view. findviewbyid (R. Id. dialog_ TV );

  

Dialogtv. settext ("ABCD ");

  

If the component R. Id. dialog_ TV is a component in the dialog box, you can directly use this. findviewbyid (R. Id. dialog_ TV) to report an error.

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.