The function of Layoutinflater.inflate () is to instantiate an XML-defined layout file as a view control object;
Differences from Findviewbyid:
Layoutinflater.inflate is to load a layout file;
Findviewbyid is to find a control from the layout file;
I. There are three ways to get Layoutinflater objects
Layoutinflater Inflater=layoutinflater.from (context);
Layoutinflater Inflater=getlayoutinflater (); can be used in activity, is actually a function of the window under the view sub-class
Layoutinflater inflater= (Layoutinflater) Context.getsystemservice (Layout_inflater_service);
Two: Inflate parameters
Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot):
The ID of the Resource:view layout
Root: You need to attach to the resource resource file, inflate () Returns a View object, and if the third argument attachtoroot true, the root is returned as the root object. Otherwise, only the Layoutparams property of the root object is appended to the root layout object of the resource object, which is the outermost view of the layout file resource. If root is null, the Layoutparams property of the view root object is ignored (note).
Attachtoroot: Whether to attach root to the root view of the layout file
Example:
Layoutinflater Inflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);
View v = inflater.inflate (r.layout.fragment, Root,false);
Or:
v = inflater.inflate (r.layout.fragment, NULL);
Problems:
Sometimes we load a custom view layout file in adapter, the layout file is set android:layout_height= "100dip", but after running the program found a little effect, similar to Layout_width and so on Android : The property settings at the beginning of the layout_ are not useful;
Because one of the methods in adapter is GetView, this returned view is a load from the XML layout, which is generally as follows:
View v = inflater.inflate (r.layout.fragment, NULL);
if (root = null) { if (DEBUG) { System.out.println ("Creating params from root:" + root); } Create layout params that match root, if supplied params = root.generatelayoutparams (attrs); if (!attachtoroot) { //Set the layout params for temp If we is not //attaching. (If We is, we use AddView, below) Temp.setlayoutparams (params); } }
It can be found in the code that the params = Root.generatelayoutparams (attrs) is not executed when root is null, and this code is to convert the layout configuration in XML to Layoutparams. In other words, we load our configured layout properties for the layout class (Framelayout, etc.) to control the size, position, alignment, etc. of the view when OnLayout. Take Framelayout as an example and look at its generatelayoutparams (attrs) method. Workaround: when using the inflate method of layoutinflate, make sure that the root parameter cannot be null, in fact, this root is the meaning of the parent view, that is, when you convert XML to a view, the parent of the view is root, If you do not want to add the view to the root, then let the third argument attachtoroot to False, or true if you want to add it.
Introduction to the parameters of Android Layoutinflater.inflate ()