Android Layout LinearLayout Sub-controls the role of weight weight analysis

Source: Internet
Author: User

About the role of the LinearLayout child control weight Android:layout_weigh parameter in Android development, there are two diametrically opposed statements about its usage on the Web:


Argument one: The greater the value, the greater the importance, the larger the space occupied;

Argument two: The greater the value, the lower the importance, the less space occupied.

Which is right? Which mistake? Or are there other explanations? Click here for a detailed analysis of the effects of weight weight parameters:


In fact, these two situations are not very accurate;

The exact explanation is that the weight permission is used to assign a dimension in the direction of the parent control-a parameter that all child controls set the size and the resulting value in that direction, and the result (which may or may be negative) is assigned to the child control as a proportion of the weight value of all weight values in a child control. The final actual size of the child control in that direction is (the control sets the size of the +weight permission assignment size (possibly negative)).


For example, in the vertical direction of the LinearLayout control F, two controls are:

A control hight = a, weight =w_a;

b control hight = B,weight=w_b;

Parent control F Actual hight = C;


The final actual dimensions of control A and control B are:

A control is actually hight_a =a+ (c (a+b)) *w_a/(w_a+w_b);

B Control actual Hight_b =b+ (c (a+b)) *w_b/(w_a+w_b);


if: w_a= 1; w_b= 2;

The

Hight_a=a+ (C-(A+B))/3;

Hight_b=a+ (C-(a+b)) *2/3;


So when (C-(A+B)) value is greater than 0 o'clock, that is, the sum of all child control heights (or widths) is less than the height (or width) of the parent control, the larger the weight permission value, the greater the value of the control's size on the original set size;

Therefore, when the (C-(A+B)) value is less than 0 o'clock, that is, the sum of all child control heights (or widths) is greater than the height (or width) of the parent control, the larger the weight permission value, the larger the size of the control is subtracted from the original size;

However, the actual size of the control is calculated as the formula above, and the control is not in all cases a weight value the larger the control occupies, or the smaller the weight value, the less space the control occupies.

So why do people generally think so? In fact, there are two kinds of special cases:

One is: All controls this direction size is set to 0DP, then the formula of a=0;b=0; the final A and B controls are in the following dimensions:

hight_a=c*w_a/(W_a+w_b);

hight_b=c*w_b/(W_a+w_b);

This is the so-called statement one: the greater the value, the higher the importance, the greater the space occupied.

The second is: all controls the direction size is set to Fillparent, then the formula of the A=c;b=c, the final A, B control dimensions are:

hight_a=c* (1-*w_a/(w_a+w_b));

hight_b=c* (1-*w_b/(w_a+w_b));

This is the so-called argument two: the greater the value, the greater the importance, the less space occupied.

At this point, there should be a more correct understanding of the role of the weight value of the controls in the LinearLayout layout:

Dimensions are calculated by formula and are generally considered only in exceptional cases: the larger the value, or the larger the value, the smaller the space.

There is one thing that I think is often not noticed, but it is also particularly useful: you can use the weight value to allocate the extra dimensions of the parent control (relative to the sum of the dimensions of all child controls in that direction) in a way that guarantees the smallest size of the space, to achieve some specific proportions of the effect, no longer unfold Interested can be calculated according to the formula, yourself to try it, should have your unexpected harvest.

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.