Android: layout_weight

Source: Internet
Author: User

This attribute is valid only in Linearlayout. Android: layout_weight is controversial because setting android: layout_width to wrap_content and match_parent at the same time results in two opposite effects. As follows:

 
         
          
  
 

In the preceding layout, the width of both textviews is set to match_parent, and the weight is 1 and 2. The result is as follows:


We can see that the weight of 1 accounts for 2/3!

Let's look at the following layout: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48cHJlIGNsYXNzPQ = "brush: java;">
That is, the width is wrap_content. The view is as follows:


TextView on the left accounts for 1/3 of the total, and it is normal again.

Android: layout_weight: once this attribute is set for a View (assuming it is valid), the width of the View is equal to the original width (android: layout_width) add the percentage of remaining space!

Set the screen width to L. If the width of both views is match_parent, the original width is L and the width of both views is L, then the remaining width is L-(L + L) =-L, and the View on the left accounts for 1/3, so the total width is L + (-L) * 1/3 = (2/3) L. in fact, the weight value of the default View is 0. Once this value is set, the reason why onMeasure is executed twice during the painting of the view.

Google officially recommends that you set width to 0 dip when using the weight attribute. The effect is the same as setting it to wrap_content. In this way, weight can be understood as the 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.