The role and use of layout_weight in the child of Android--linearlayout

Source: Internet
Author: User

When a child with weight is included in the LinearLayout, LinearLayout will measure two times:

First measure the original value of the child:

The second measurement of a child's specific gravity value:

The value of the 2 measurements is then added to get the specific width or height of the child.

//-----------------------------------------------------------------------------

We assume that a linearlayout direction is horizontal android:orientation= "horizontal", which has 2 child, child_1 and Child_2, respectively.

Let's analyze how the values of these 2 child widths are calculated with the Layout_weight attribute.

The first measurement: has its own layout_width parameter decision

First, the child's width (layout_width) has 3 cases: 0DP (specific values, can be set arbitrarily, such as 100DP), Wrap_content, Match_parent.

In these 3 cases, when the application is loaded on a specific phone, it will be measured to a specific value, as follows:

1, when Child_1 's Layout_width is 0DP, its width_orginal original width is 0pix; (100DP, then width_orginal for screen density density * pix = x_1 pix)

(where the cell phone screen density is through context.getresources (). Getdisplaymetrics (). Density get)

2, when Child_1 's Layout_width is wrap_content, its width_orginal original width is int_wrap_cotent (on the specific phone is a fixed value, assuming x_1 pix);

3, when Child_1 's Layout_width is match_parent, its width_orginal original width for the width of the phone screen (on the specific phone is a fixed value, assuming x_1 pix);

(The original width of the child_2 is x_2)

Second measurement: All child's layout_weight parameters are determined together

Assume that the layout_weight of the 2 child are weight_1 and weight_2 (the Layout_weight parameter is an int type value, which can be 0):

Then the final width of child_1 and child_2 is:

Child_1 width: x_1 + (Phone screen width-(x_1 + x_2)) * Weight_1/(weight_1+weight_2) = final width.

Here are a few examples:

The layout_width of Demo1:child_1 and Child_2 are 0dp,layout_weight 1 and 2 respectively.

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/root"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "Horizontal" >7 8     <TextView9         Android:id= "@+id/tv_left"Ten Android:layout_width= "0DP" One Android:layout_height= "Wrap_content" A Android:layout_weight= "1" - Android:background= "#00ff00" the Android:text= "Child_1" - android:textsize= "24SP" /> -  -     <TextView +         Android:id= "@+id/tv_right" - Android:layout_width= "0DP" + Android:layout_height= "Wrap_content" A Android:layout_weight= "2" at Android:background= "#ff0000" -  - Android:text= "Child_2" - android:textsize= "24SP" /> -  - </LinearLayout>

At this point, the final width of the child_1 is: 0 pix + screen width * 1/3 = 1/3 screen width. Child_2 the screen width of the final width of 2/3.

So you want to proportionally distribute the wide (high) value of the LinearLayout children, you can use the secondary method.

The layout_width of Demo2:child_1 and Child_2 are 100dp,layout_weight 1 and 2 respectively.

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/root"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "Horizontal" >7 8     <TextView9         Android:id= "@+id/tv_left"Ten Android:layout_width= "100DP" One Android:layout_height= "Wrap_content" A Android:layout_weight= "1" - Android:background= "#00ff00" -  the Android:text= "Child_1" - android:textsize= "24SP" /> -  -     <TextView +         Android:id= "@+id/tv_right" - Android:layout_width= "100DP" + Android:layout_height= "Wrap_content" A Android:layout_weight= "2" at Android:background= "#ff0000" -  - Android:text= "Child_2" - android:textsize= "24SP" /> -  - </LinearLayout>

At this point, the final width of the child_1 is: + * density pix + (screen width-DP * density) * 1/3 = ...

The layout_width of Demo2:child_1 and Child_2 are match_parent,layout_weight 1 and 2 respectively.

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:id= "@+id/root"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 android:orientation= "Horizontal" >7 8     <TextView9         Android:id= "@+id/tv_left"Ten Android:layout_width= "Match_parent" One Android:layout_height= "Wrap_content" A Android:layout_weight= "1" - Android:background= "#00ff00" -  the Android:text= "Child_1" - android:textsize= "24SP" /> -  -     <TextView +         Android:id= "@+id/tv_right" - Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" A Android:layout_weight= "2" at Android:background= "#ff0000" -  - Android:text= "Child_2" - android:textsize= "24SP" /> -  - </LinearLayout>

At this point, the final width of the child_1 is: Screen width + (screen width-twice times the screen width) * 1/3 = 2/3 screen width, child_2 1/3 screen width.

When the layout_width of child is match_parent, want child_1:child_2 = 1:2 (i.e. 1/3:2/3), you can set the child_2 of Child_1 Layout_weight as ( Denominator-1): (Denominator-2).

When the child's layout_width are match_parent, want child_1:child_2:child_3 = 3:4: 5 (that is, 3/12:4/12:5/12), you can set the Layout_weight (12-3):(12-4):(12-5)

The role and use of layout_weight in the child of Android--linearlayout

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.