I answer the same question (continued two)

Source: Internet
Author: User

This piece of article from Http://blog.csdn.net/andywuchuanlong, reproduced please explain the source, thank you!

I'll answer the question. Serial Directory http://blog.csdn.net/andywuchuanlong/article/details/44194043

3, when using view.inflate (context, resource, root) to load the layout file, if root is null, why does the layout file's root node set properties such as margin, height, etc. do not work?

When analyzing this problem, we need to analyze it from far source. assuming that the parameter root is null , inflater.inflate (r.layout.item_list, parent,false); The method is called as follows:

<span style= "Color:rgb (51, 51, 51); >public View Inflate (xmlpullparser parser, ViewGroup root, Boolean attachtoroot) {</span><span style= "color : #ff0000; " >//attachtoroot false</span><span style= "color: #333333;"            > synchronized (Mconstructorargs) {final AttributeSet attrs = xml.asattributeset (parser);            Context Lastcontext = (context) mconstructorargs[0];          Mconstructorargs[0] = Mcontext; </span><span style= "color: #ff0000;" > View result = root;//is actually resutl = Null</span><span style= "color: #333333;"                > try {//Look for the root node.                int type; while (type = Parser.next ())! = Xmlpullparser.start_tag && Type! = Xmlpullparser.end_docum                    ENT) {//Empty} if (type! = Xmlpullparser.start_tag) { throw New Inflateexception (PARSER.GETPOSITIONDESCRIPtion () + ": No start tag found!");                } final String name = Parser.getname (); if (tag_merge.equals (name)) {if (root = null | |!attachtoroot) {throw new INF Lateexception ("<merge/> can be used only with a valid" + "ViewGroup root and attach                    Toroot=true ");                } rinflate (parser, Root, attrs, false);                    } else {//temp is the root view and was found in the XML view Temp;                    if (tag_1995.equals (name)) {temp = new Blinklayout (Mcontext, attrs);                    } else {temp = Createviewfromtag (root, name, attrs);                    } 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.generat                        Elayoutparams (ATTRS); if (!attachtoroot) {</span><span style= "color: #ff0000;" >//true</span><span style= "color: #333333;" >//Set the layout params for temp if we is not//attaching.                        (If We are, we use AddView, below) temp.setlayoutparams (params); }} if (DEBUG) {System.out.println ("-----> Start Inflatin                    G Children ");                    }//Inflate all children under temp rinflate (parser, temp, attrs, true);           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//top view found I                    n XML.                    if (root = null | |!attachtoroot) {result = temp; }}} catch (Xmlpullparserexception e) {inflateexception ex = new Inflateexcepti                On (E.getmessage ());                Ex.initcause (e);            Throw ex; } catch (IOException e) {inflateexception ex = new Inflateexception (Parser.getposi                Tiondescription () + ":" + e.getmessage ());                Ex.initcause (e);            Throw ex;          } finally {      Don ' t retain static reference on context.                Mconstructorargs[0] = Lastcontext;            MCONSTRUCTORARGS[1] = null;        } return result; }}</span>

The code is long and we focus on the following code

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);      }    }
The code means that when we pass in the root parameter is not empty, and Attachtoroot is false, that is, the implementation of the above twoactivity, will give the temp set a layoutparams parameter. So what does this temp do?
<pre name="Code" class="Java">//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;  }
It should be understood now that when we pass in the root is not NULL, and the third argument is false, the temp is added to root, and the root is returned as the final return value. And when we set the root to empty, there is no settingThe Temp object of the Layoutparams parameter, returned as a return value.

Therefore, we can draw the following conclusions:

1. If we use Convertview = Inflater.inflate (r.layout.item_list, NULL), the mode fills the view, and the Layout_xx property of the root view in the item layout is ignored. Then set the default package content mode

2. If we want to ensure that the parameters in the item's view are not changed, we need to use Convertview = Inflater.inflate (r.layout.item_list, parent,false); this way the view fills

3. In addition to using this approach, we can also set the item layout's root view as the package content, and then set properties such as the height of the internal controls so that the display is not modified.

Briefly summarize:

When Root is null, we simply instantiate an XML file as a view object, and the corresponding view of the XML is returned. When Root is not empty, that is, the parent exists, the materialized view object is added to the parent and then returned

I answer the same question (continued two)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.