Android:layout_height and other properties in the Android layout file do not work

Source: Internet
Author: User
<span id="Label3"></p><p><p>sometimes, We configure a layout file that is found after loading is added to our activity, and does not have the properties set by US installed</p></p><p><p>To layout, than for us to set the android:layout_margintop= "100dip", but after running the program found a little effect, similar to layout_height and so on Android:layout_ the beginning of the property settings have no effect, This kind of problem is mostly used when we use adapter as the data source, because there is a method in adapter that is getview, the returned view is a load from the XML layout, generally as Follows:</p></p><p><p></p></p>Java code <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li><span class="keyword">If (convertview==<span class="keyword">Null) {</span></span></li> <li>Convertview=layoutinflater.from (mcontext). Inflate (r.layout.main, <span class="keyword">null); </span></li> <li>}</li> <li><span class="keyword">return convertview; </span></li> </ol> </ol>The problem is in our layoutinflater.from (mcontext). inflate (r.layout.main, null); this code, when using inflate, if the second argument (View Root) is null, Then it will not load the layout-related configuration of the topmost layout node in your layout file (that is, attributes that begin with android:layout_). We can look at the implementation of this method to illustrate, by looking up the source code, the implementation of inflate in this public View inflate (xmlpullparser parser, viewgroup root, Boolean Attachtoroot) method is defined in The. One segment: Java code <ol class="dp-j" start="1"> <li><li><span class="keyword">If (root = <span class="keyword">Null) {</span></span></li></li> <li><li><span class="keyword">if (DEBUG) {</span></li></li> <li><li>System.out.println (<span class="string">"Creating params from root:" +</span></li></li> <li><li>root);</li></li> <li><li>}</li></li> <li><li><span class="comment">//Create layout params that match root, if supplied</span></li></li> <li><li>params = Root.generatelayoutparams (attrs);</li></li> <li><li><span class="keyword">if (!attachtoroot) {</span></li></li> <li><li><span class="comment">//Set the layout params for temp if we</span> is not</li></li> <li><li><span class="comment">//attaching. (If We is, we use addview, below)</span></li></li> <li><li>Temp.setlayoutparams (params);</li></li> <li><li>}</li></li> <li><li>}</li></li> </ol>As you can see, when Root is null, the params = Root.generatelayoutparams (attrs) is not executed, 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. Java code <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li> <span class="keyword">public layoutparams generatelayoutparams ( Attributeset attrs)  {   </span> </li> <li>         < Span class= "keyword" >return <span class="keyword">new framelayout.layoutparams (getcontext (),  attrs);            </span> </li> <li>      }   </li> </ol> </ol>Very simply, constructs a framelayout.layoutparams class, this class integrates the marginparams, adds a gravity to its property configuration ... here, If you want to customize your own viewroup, and the ViewGroup has some property settings for the custom control layout, You can extend the layout configuration by integrating the view.marginparams, and then override the Generatelayoutparams method so that the system framework automatically uses that layout to read the layout properties we have configured in XML to control the location of our VIEW. Based on the above analysis, we must ensure that the root parameter cannot be null when using Layoutinflate's inflate method, in fact, this root is the meaning of the parent view, that is, when you convert the XML into a view, The parent of the view is root, and 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. Speaking of this problem, in fact, there are some layout, their parameter configuration to meet certain conditions will work, such as Framelayout in the view, you want its leftmargin to take effect, you must specify its layout_gravity as left, Likewise right corresponds to Rightmargin.top and bottom. Be careful when you use it, and look at the source code More. Otherwise it will be nameless name, do not know where the Problem. ViewGroup three lines onmeasure measure the size of the view OnLayout control the layout of the view draw draws the View,drawchild the child view<p><p>Android:layout_height and other properties in the Android layout file do not work</p></p></span>

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.