The root node of the XML file Layout_width or Layout_height set no effect on the cause analysis

Source: Internet
Author: User

In the Android development is believed that the ListView, the GridView and so on the formation all very familiar, when uses them to need to configure the related adapter, and configures the present backbone XML file as the ListView and so on the formation child view, These XML files are called in the GetView method of adapter. For example:

Public View GetView (int position, View Convertview, ViewGroup parent) {        if (convertview==null) {            Convertview = App . Getlayoutinflater (). Inflate (R.layout.item, null);        }                return convertview;    }
The Item.xml file is as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" 400DP    " Android:background= "#008000"     >        <imageview            android:src= "@drawable/ic_launcher"            android: Layout_width= "Match_parent"            android:layout_height= "match_parent" >        </imageview></ Relativelayout>

Using the method above, you will find that the effect is always the same regardless of the layout_width and layout_height settings of the root view or relativelayout. In other words, you want to change the size of the inside ImageView by Relativelayout, the usual solution is to add a view to the ImageView package. You can change the size of the ImageView by setting the view size (note that it is not necessarily imageview, or it may contain several view):

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" 400DP    " Android:background= "#008000"     >    <relativelayout        android:layout_width= "900DP"        android: layout_height= "200DP"        android:background= "@android: Color/black" >        <imageview            android:src= "@ Drawable/ic_launcher "            android:layout_width=" match_parent "            android:layout_height=" Match_parent ">        </ImageView>    </RelativeLayout></RelativeLayout>
Although the solution has been found, but also know the reason why, why good? A conceptual question to understand before the analysis:

layout_width not width! Layout_height is not height!. that is to say that these two properties are set not the width and height of the view, layout is the meaning of layouts, that is, the two properties are view in the layout and height ! Since it is a layout, there must be a place where the view is placed, that is, there is a medium to place the view, and a layout_width and a layout_heigth size on that medium to place the view. What if the media view layout is not there? So the underlying reason for the above problem is that you did not set a layout medium for the XML file (the medium is also a view, that is, Rootview), So in order to protect the layout_width and layout_heigth of the root view in your item.xml, you need to set up a medium like this. This root is a medium in code inflate (Int,viewgroup root), but is usually passed as null, Therefore, the root view of the Item.xml file is not based on the layout of the media, so it does not work; Since the cause of the problem has been found then you can use a stupid way to solve the problem: to provide a root for inflate, in the code I simply do a bit of processing, to verify their ideas:

Public View GetView (int position, View Convertview, ViewGroup parent) {if (convertview==null) {Convertview = App.getlayout Inflater (). Inflate (R.layout.item, null);                        Set up a root manually, at which point the layout_width or layout_height will work                        Convertview = App.getlayoutinflater (). Inflate ( R.layout.item, (ViewGroup) convertview);} return Convertview;}
The reason for this method is that it makes two XML parsing of item because inflate is called two times. Of course the item at this time is:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" 400DP    " Android:background= "#008000"     >        <imageview            android:src= "@drawable/ic_launcher"            android: Layout_width= "Match_parent"            android:layout_height= "match_parent" >        </imageview></ Relativelayout>

Since it's about the second root of the inflate, look at what Root has done and trace the source code to finally parse the XML in the following way:

Remember that at this point root is null,attachtoroot in the source code for Root!=null, so here is Falsepublic View inflate (xmlpullparser parser, ViewGroup root,            Boolean attachtoroot) {synchronized (Mconstructorargs) {...  View result = root;  Root view, null try {.... final String name = Parser.getname ();                     The name of the node, if it is a custom view, is the fully qualified name//The IF statement does not see if (tag_merge.equals (name)) {//Process <merge/> Label ...                } else {//Temp is the root view and is found in the XML//This is the XML file that corresponds to the root view, in                         The Item.xml file is relativelayout View temp = Createviewfromtag (name, attrs);                      Viewgroup.layoutparams params = null; Because the root==null,if condition does not set if (root = null) {//Create layout params that match Root, if supplied//get a Layoutparams instance based on the AttributeSet property, remember that the caller is root.                           params = Root.generatelayoutparams (attrs);                              if (!attachtoroot) {//Reset temp layoutparams//Set the layout params for temp if we is not Attaching.                          (If We are, we use AddView, below) temp.setlayoutparams (params); }}//Inflate all children under temp//traverse all child nodes under temp, i.e. X                                            ML files with all the words in the file View rinflate (parser, temp, attrs); The root node of the XML file and the view of the child nodes in the root node are added to root, of course, if Root equals null here is not executed if (root! = NULL && attachtoroot                      ) {Root.addview (temp, params);                          }//If the root node is null, the root node in the XML and the child nodes of the root node are returned directly with the view if (root = null | |!attachtoroot) {                result = temp;      }}} ... return result;   }      }
By analyzing the above code we can see that when root is null, inflate directly returns the view generated by the XML file, and the view root view returned is the root node of the XML file. At this point the view does not exist in any layout, so the layout_height and layout_width of the root node have no dependent media, resulting in the setting of these two properties is invalid. But if Root is not NULL, the view returned by inflate is a view that first parses the XML into a view and then adds the view to root by Root.addview, and then returns to root directly. In other words, the layout_width and layout_height of the root node in the XML file are the width and height of the layout in root, so these two properties are effective at this time.

Note that in activity we often call Setcontentview (int layoutresourceid) to set the activity's view, why the Layout_width and layout_ of the root nodes of these XML files Does height have effect? And look at the following analysis, the analysis of source code discovery Setcontentview actually called the inflate method to parse the XML file:

    There is a window reference in activity that calls Window.setcontentview in the activity's Setcontentview, which refers to Phonewindow, This snippet is code in the Phonewindow class @override public    void Setcontentview (int layoutresid) {       if (mcontentparent = = null) {            //The mcontentparent is initialized in this method to ensure that it is not null           Installdecor ();        } else {            mcontentparent.removeallviews ();        }       //The code here is familiar, formally like the above adapter, except that at this point root is NOT NULL!!!!       Mlayoutinflater.inflate (Layoutresid, mcontentparent);        Final Callback cb = Getcallback ();        if (CB! = null) {            cb.oncontentchanged ();        }    }

You can see that there is a mcontentparent in the method that first determines whether mcontentparent is null, and if NULL, it is initialized in the Installdecor () method. That is, mcontentparent can always be initialized, followed by a call to the inflate (layoutresid,mcontentparent) method, You can see that the second parameter in inflate that represents root is mcontentparent, and is not NULL. Then execute the method to parse the XML file, and eventually execute the above Inlfate (Xmlpullparser,root,attachtoroot) method, according to the above analysis, So it can be concluded that the root node of XML in activity is set layout_width or layout_height is effective!

The root node of the XML file Layout_width or Layout_height set no effect on the cause analysis

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.