Original address: http://lmbj.net/blog/layoutinflater-and-layoutparams/
View view = Inflater.inflate (R.layout.item, NULL);
One of the problems you will encounter when using the same method as above to get a view is that the layoutparams defined in the layout file is ignored. Here are three StackOverflow questions:
Http://stackoverflow.com/questions/5288435/layout-params-of-loaded-view-are-ignored
Http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater
Http://stackoverflow.com/questions/2738670/layoutinflater-ignoring-parameters
All say that it is possible to use:
/* resource id */, parent /* parent */,false /*attachToRoot*/);
At this point the problem has been resolved, but none of the three questions mentions why this invocation is the only line.
Interested in our source to find the answer, Layoutinflater of the inflate method, the last is called the same method:
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
The reason is naturally in this method, it probably means that only viewgroup root is not empty to read the view's layoutparams. attachToRoot = true
, the view is added to root. And most of the situation does not want to be addview to root, naturally to be assigned to Flase, this is the solution above.
You can see the source code related to the inflate method of Layoutinflater:
/** * Inflate a new view hierarchy from the specified XML resource. Throws * {@link inflateexception} If there is an error. * * @param resource ID for a XML layout resource to load (e.g., * <code>r.layout.main_page</code& gt;) * @param root Optional view to be the parent of the generated hierarchy. * @return The root View of the inflated hierarchy. If Root was supplied, * This is the root View; Otherwise it is the root of the inflated * XML file. */Public ViewInflate(int resource, ViewGroup root) {Return inflate (resource, root, root! =NULL); }/** * Inflate a new view hierarchy from the specified XML node. Throws * {@link inflateexception} If there is an error. * * <p> * <em><strong>Important</strong></em> For performance * reasons, view inflation relies heavily on pre-processing of the XML files * That's done at build time. Therefore, it is not currently possible to * use Layoutinflater with an xmlpullparser over a plain XML file at runtime. * * @param parser XML DOM node containing the description of the view * hierarchy. * @param root Optional view to be the parent of the generated hierarchy. * @return The root View of the inflated hierarchy. If Root was supplied, * This is the root View; Otherwise it is the root of the inflated * XML file. */Public ViewInflate(Xmlpullparser parser, ViewGroup root) {Return inflate (parser, root, root! =NULL); }/** * Inflate a new view hierarchy from the specified XML resource. Throws * {@link inflateexception} If there is an error. * * @param resource ID for a XML layout resource to load (e.g., * <code>R.layout.main_page</code>) * @param R Oot Optional view to being the parent of the generated hierarchy (if * <em>attachToRoot</em> is true), or else si Mply an object, provides a set of layoutparams values for root of the returned * hierarchy (if <em>attachtoroo T</em> is false.) * @param attachtoroot Whether The inflated hierarchy should be attached to * the root parameter? If false, Root is only used to create the * correct subclass of Layoutparams for the root view in the XML. * @return The root View of the inflated hierarchy. If Root was supplied and * Attachtoroot are true, this is root; Otherwise it is the root of * the inflated XML file. */Public ViewInflate(int resource, ViewGroup root, Boolean attachtoroot) {if (DEBUG) System.Out.println ("Inflating from Resource:" + Resource); Xmlresourceparser parser = GetContext (). Getresources (). GetLayout (Resource);try {Return inflate (parser, Root, attachtoroot); }finally {parser.close ();}}/** * Inflate a new view hierarchy from the specified XML node. Throws * {@link inflateexception} If there is an error. * <p> * <em><strong>Important</strong></em> For performance * reasons, view inflation relies heavily on pre-processing of the XML files * That's done at build time. Therefore, it is not currently possible to * use Layoutinflater with an xmlpullparser over a plain XML file at runtime. * * @param parser XML DOM node containing the description of the view * hierarchy. * @param root Optional view to being the parent of the generated hierarchy (if * <em>attachToRoot</em> is true), Or else simply an object, provides a set of layoutparams values for root of the returned * hierarchy (if <em>a Ttachtoroot</em> is false.) * @param attachtoroot Whether The inflated hierarchy should be attached to * the root parameter? If false, Root is only used to create the * correct subclass of Layoutparams forThe root view in the XML. * @return The root View of the inflated hierarchy. If Root was supplied and * Attachtoroot are true, this is root; Otherwise it is the root of * the inflated XML file. */Public ViewInflate(Xmlpullparser parser, ViewGroup root, Boolean attachtoroot) {synchronized (Mconstructorargs) {final AttributeSet attrs = xml.asattributeset (parser); mconstructorargs[0] = Mcontext; View result = root;try {Look for the root node.int type;while (type = Parser.next ())! = Xmlpullparser.start_tag && Type! = xmlpullparser.end_document) {Empty}if (type! = Xmlpullparser.start_tag) {ThrowNew Inflateexception (parser.getpositiondescription () +": No start tag found!"); } final String name = Parser.getname ();if (DEBUG) {System.Out.println ("**************************"); System.Out.println ("Creating root view:" + name); System.Out.println ("**************************"); }if (tag_merge.equals (name)) {if (root = =null | | !attachtoroot) {ThrowNew Inflateexception ("<merge/> can be used only with a valid" +"ViewGroup Root and Attachtoroot=true"); } rinflate (parser, Root, attrs); }else {Temp is the root view and was found in the XML view temp = Createviewfromtag (name, attrs); Viewgroup.layoutparamsparams =Nullif (Root! =NULL) {if (DEBUG) {System.Out.println ("Creating params from root:" + root);}Create layout params that match root, if suppliedparams = Root.generatelayoutparams (attrs);if (!attachtoroot) {Set the layout params for temp if we is notAttaching. (If We are, we use AddView, below) temp.setlayoutparams (params); } }if (DEBUG) {System.Out.println ("-----> Start inflating Children"); }Inflate all children under temp rinflate (parser, temp, attrs);if (DEBUG) {System.Out.println ("-----> Done inflating Children"); }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 //to P view found in XML. if (root = null | |!attachtoroot) {result = temp;}}} catch (xmlpullparserexception e) {inflateexception ex = new Inflateexception (E.getmessage ()); Ex.initcause (e); throw ex;} catch (IOException e) {inflateexception ex = new Inflateexception (parser.getpositiondescription () + ":" + e.getmessage ()); Ex.initcause (e); throw ex;} return result; }
[Android] Turn-layoutinflater lost view Layoutparams