Android: Layout_weight correct understanding,

Source: Internet
Author: User

Android: Layout_weight correct understanding,
Preface

Original article. You are welcome to repost it. Please keep the source. If you have any errors, questions, or suggestions, please note them. My mailbox: Maxwell_nc@163.com

Today, when I was reading a book, I accidentally found that "android: Layout_weight is the control that occupies the screen importance, and the smaller the value, the higher the importance". At that time, I suddenly felt that something was wrong. android: layout_weight should be the screen weight of the control. The larger the value, the larger the proportion. After reading the information, I wrote a blog to record the summary and sharing.

Incorrect understanding
  • Android: the greater the value of Layout_weight, the greater the weight, the larger the proportion
  • Android: the smaller the value of Layout_weight, the larger the weight, the larger the proportion
  • Android: Layout_weight
    • The larger the general value, the larger the weight, the larger the proportion
    • When width or height is fill_parent or match_parent, the smaller the value, the larger the weight.

These are all incorrect understandings. For example, the first misunderstanding is that when writing android: Layout_weight, the width or height is set to 0dip, the second is to set the width or height to fill_parent or match_parent, and the third is not to come to any conclusion.

Correct understanding

The correct understanding is:

Android: Layout_weight is the weight of the control to occupy the screen. You can use the formula to calculate the correct proportion to occupy the screen.

The following formula uses width as an example:

Actual width = control width + remaining width * (control weight/total weight)
  • Scenario 1(When width is 0dp ):
    Assume that there are two controls. The first weight is 1 and the second weight is 2. The total weight is 3,
    The first actual width = 0 + screen width * (1/3) = 1/3 screen width
    The second actual width = 0 + screen width * (2/3) = 2/3 screen width

  • Case 2(When width is fill_parent or match_parent ):
    Assume that there are two controls. The first weight is 1 and the second weight is 2. The total weight is 3,
    The first actual width = screen width + (-1 * screen width) * (1/3) = 2/3 screen width
    The second actual width = screen width + (-1 * screen width) * (2/3) = 1/3 screen width

Note: remaining width = screen width-width of the first control-width of the second control = (-1 * screen width)

In this case, the larger the weight, the smaller the proportion occupied.

  • Case 3(When width is wrap_content or custom value ):
    It can be entered according to the formula.
Summary

In fact, when Google defines android: Layout_weight, we hope we can quickly divide the layout by proportion. Therefore, we recommend that you use android: when Layout_weight is used, the width and height are set to 0dp (when 0dp is used, the larger the weight ratio is), facilitating calculation.

References

Http://mobile.51cto.com/abased-375428.htm

Related Article

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.