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
- static method via Layoutinflater from ()
Layoutinflater inflater = Layoutinflater.from (this); View Viewnull);
- Get through the service
Layoutinflater inflater = (layoutinflater) Context.getsystemservice (context.layout_inflater_service);
- 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