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.