The true meaning of android:layout_weight

Source: Internet
Author: User

First declare that the property is valid only in LinearLayout. The reason why Android:layout_weight is controversial is that while setting this property, setting Android:layout_width to Wrap_content and match_parent can result in two diametrically opposed effects. As shown below:

<linearlayout        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        Android : orientation= "Horizontal" >        <textview            android:layout_width= "match_parent"            android:layout_ height= "Wrap_content"            android:layout_weight= "1"            android:background= "@android: Color/black"            android: text= "111"            android:textsize= "20sp"/>        <textview            android:layout_width= "Match_parent"            android:layout_height= "Wrap_content"            android:layout_weight= "2"            android:background= "@android: color/ Holo_green_light "            android:text=" 222 "            android:textsize=" 20sp "/>

The above layout sets the width of the two textview to Match_parent, a weight of 1, and a weight of 2. The results are as follows:


You can see that the weight is 1, but it accounts for Two-thirds!

Let's look at the following layout:

    <linearlayout        android:layout_width= "match_parent"        android:layout_height= "Wrap_content"        Android : orientation= "Horizontal" >        <textview            android:layout_width= "wrap_content"            android:layout_ height= "Wrap_content"            android:layout_weight= "1"            android:background= "@android: Color/black"            android: text= "111"            android:textsize= "20sp"/>        <textview            android:layout_width= "Wrap_content"            android:layout_height= "Wrap_content"            android:layout_weight= "2"            android:background= "@android: color/ Holo_green_light "            android:text=" 222 "            android:textsize=" 20sp "/>    </LinearLayout>

The width is wrap_content and the view is as follows:


The left TextView accounted for One-third, and normal again.

The true meaning of android:layout_weight is that once the view has this property set (assuming it is valid), the view width is equal to the original width (android:layout_width) plus the amount of space remaining!

Set the screen width to L, the width of the two view is Match_parent, the original width is L, two of the view width is L, then the remaining width is L (l+l) =-L, the left view accounted for One-third, so the total width is L + (-l) *1/3 = (2/ 3) L. In fact, the default view of weight This value is 0, once set this value, then the view at the time of drawing execution onmeasure two times the reason is here.

Google's official recommendation, when using the weight attribute, set the width to 0dip, the effect is the same as set to Wrap_content. So weight can be understood as a proportion!


The true meaning of android:layout_weight

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.