This article details the properties of Layout_weight in the Android layout, which is the weight of the remaining space .
First look at the role of the Layout_weight property: It is used to allocate a property of the remaining space, you can set his weight. Many people do not know what the remaining space is a concept, let me say the rest of the space.
1. Preliminary understanding
Let's look at the following code:
<?XML version= "1.0" encoding= "Utf-8"?> <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Left"Android:text= "One"/> <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:layout_weight= "1.0"Android:text= "both"/> <EditTextAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:gravity= "Right"Android:text= "three"/> </LinearLayout>
View Code
The operating result is:
Look at the code above: only EditText2 uses the Layout_weight attribute and assigns values to 1, while EditText1 and EditText3 are not set, according to the API, they are 0 by default.
Layout_weight the real meaning of this property:
The Android system is based on the 3 EditText height layout_height values you set, assigning them 3 heights,
Then the rest of the screen space will be assigned to Button2, because only his weight value is 1, which is why Button2 accounted for such a large piece of space.
Now we have a preliminary understanding of the Layout_weight attribute, and we'll learn more about it below.
2. Further information
Let's look at the following code:
<? XMLversion= "1.0"encoding= "UTF-8"? > <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextViewAndroid:background= "#ff0000"Android:layout_width="**"Android:layout_height= "Wrap_content"Android:text= "1"Android:textcolor= "@android:color/white"Android:layout_weight= "1"/> <TextViewAndroid:background= "#cccccc"Android:layout_width="**"Android:layout_height= "Wrap_content"Android:text= "2"Android:textcolor= "@android:color/black"Android:layout_weight= "2" /> <TextViewAndroid:background= "#ddaacc"Android:layout_width="**"Android:layout_height= "Wrap_content"Android:text= "3"Android:textcolor= "@android:color/black"Android:layout_weight= "3" /> </LinearLayout>
View Code
(1) When three textview were layout_width="wrap_content " and Layout_weight were three , you get the following effect:
(is the remaining space 1:2:3)
According to the above understanding, the system first assigns to 3 TextView their width value wrap_content (width enough to contain their content), and then the remaining space according to the 1:2:3 column assigned to 3 TextView, so the above image appears.
(2) When three TextView are layout_width="fill_parent" and Layout_weight are 1, 2 respectively, 2 O'Clock , you will get the following effect:
you will find that the weight of 1 is small, but more points, this is why??? According to the above understanding we analyze:
The system assigns 3 TextView the width fill_parent they want, which is the width of the screen.
Then the remaining space ra=1 a parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width)
Then the actual width of the first TextView =parent_width + 1/5 * ( -2 parent_width) =3/5parent_width
Similarly the actual width of the second TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width;
The actual width of the third TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width;
So it's 3:1:1 's Billy Show.
(3) When three TextView are layout_width="fill_parent" and Layout_weight are 1 respectively ,2,3 , you get the following effect:
The third one directly does not show, why? Let's take a look at the above method to calculate it:
Remaining Space =parent_width-3*parent_width=-2 a parent_width
The actual width of the first TextView =parent_width +1/6* ( -2parent_width) =2/3parent_width;
The actual width of the second TextView =parent_width +2/6* ( -2parent_width) =1/3parent_width;
The actual width of the third TextView =parent_width +3/6* ( -2parent_width) =0parent_width;
So it's 2:1:0 's Billy Show. The third one has no space.
Transferred from: http://mobile.51cto.com/abased-375428.htm
Android:layout_weight's deep Understanding (Turn)