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 will cause controversy,

is to set Android:layout_width to Wrap_content and match_parent while setting this property

will result in two diametrically opposed effects.

The true meaning of android:layout_weight is that once the view has set the attribute (assuming it is valid),

Then the width of the view is equal to the original width (android:layout_width) plus the proportion of the remaining space!

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 weight This value is 0,

Once this value is set, the reason for the view to execute onmeasure two times at the time of drawing 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!

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.