The inflate method of layoutinflater is often used in the Oncreateview method of fragment:
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
There are four kinds of inflate methods in Layoutinflater, but these are the only two that we use frequently in our daily use:
Public View Inflate (int resource, ViewGroup root) { return inflate (resource, root, root! = null); }
Public View Inflate (int resource, ViewGroup root, Boolean attachtoroot) { if (DEBUG) System.out.println ("Inflating fr Om resource: "+ Resource); Xmlresourceparser parser = GetContext (). Getresources (). GetLayout (Resource); try { return inflate (parser, Root, attachtoroot); } finally { parser.close ()} }
So, here is a direct introduction to this method can be called:
Public View Inflate (Xmlpullparser parser, ViewGroup root, Boolean attachtoroot)
In this method, the upper part of the XML parsing code, which is not posted here, there is really nothing to see. Look directly at the middle part of the code:
Viewgroup.layoutparams params = 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); } }
params = Root.generatelayoutparams (attrs);
This paragraph means: If the inflate method is called and the ViewGroup root parameter is passed in, the layoutparams of Layout_width and Layout_height will be obtained from Root. If the attachtoroot is set to False, the Layoutparams will be set for the view that we loaded.
Then look down:
We is supposed to attach all the views we found (Int. temp) //to root. Do it now. if (root = null && attachtoroot) { Root.addview (temp, params); } Decide whether to return the root is passed in or the //top view found in XML. if (root = null | |!attachtoroot) { result = temp; }
Root.addview (temp, params);
If the ViewGroup root parameter is set and Attachtoroot is set to True, the view we load is added to the root view as a child view.
If we viewgroup root is set to NULL, we return directly to the view we created, and if root is not empty and Attachtoroot is set to false, return to the above paragraph: set the Layoutparams for the view we loaded.
The above is the method content, you may still have a little to see, the next article, I will do a few examples, to specify.
The inflate method of Android programming Layoutinflater