This article details the properties of Layout_weight in the Android layout, which is used to assign a property that belongs to space, and you can set his weight.
AD:
Recently wrote the demo, suddenly found layout_weight this attribute, found on the internet there are many interesting discussion about this attribute, but found a lot of information can not find a clear, so that their own combination of online data research, finally solved, write to share with you.
First look at the role of the Layout_weight property: It is used to assign a property that belongs to space, and 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.
Look at the following code:
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="left"
- android:text="one"/>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_weight="1.0"
- android:text="/> "
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="right"
- android:text="three"/>
- </linearlayout>
The operating result is:
Look at the code above: only Button2 Use the Layout_weight property, and assign values to 1, and Button1 and Button3 do not set layout_weight this property, according to the API, they are 0 by default
Now I'm going to say, layout_weight the real meaning of this property: The Android system is based on the 3 button height layout_height values you set wrap_content, assigning you the height of their 3,
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.
With the above understanding, we can have a clear understanding of the more puzzling effect of the layout_weight on the Internet.
Let's take a look at this piece of code:
- <? XML version= "1.0" encoding= "UTF-8"? >
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="Horizontal" >
- <TextView
- android:background="#ff0000"
- android:layout_width="* *"
- android:layout_height="wrap_content"
- android:text="1"
- android:textcolor="@android:color/white"
- android:layout_weight="1"/>
- <TextView
- android:background="#cccccc"
- android:layout_width="* *"
- android:layout_height="wrap_content"
- android:text="2"
- android:textcolor="@android:color/black"
- android:layout_weight="2" />
- <TextView
- android:background="#ddaacc"
- android:layout_width="* *"
- android:layout_height="wrap_content"
- android:text="3"
- android:textcolor="@android:color/black"
- android:layout_weight="3" />
- </linearlayout>
When all three text boxes are layout_width= "wrap_content " , you get the following effect
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 screen space according to the 1:2:3 column assigned to 3 TextView, so the above image appears.
And when layout_width="fill_parent" , if the three TextView set their Layout_weight 1, 2, 2, it will appear the following effect:
You will find that the weight of 1 is small, but more points, this is why??? Many people on the internet said that when layout_width="fill_parent" , the smaller the value of weighth, the higher the priority, as if the back of the formula
, in fact, they do not really understand the problem, the real reason is layout_width= "fill_parent" cause. According to the above understanding we analyze:
The system assigns 3 TextView to their desired width fill_parent, which means that each is filled with his parent control, where the width of the dead screen
Then the remaining space at this time = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width)
Then the actual occupied width of the first textview should be =fill_parent width, i.e. parent_width + the weight of his remaining space than column 1/5 * remaining space size ( -2 parent_width) =3/5parent_width
In the same vein the actual occupied width of the second TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width;
The actual occupied width of the third TextView =parent_width + 2/5* ( -2parent_width) =1/5parent_width; so 3:1:1 Billy shows.
Then you will understand why when you set three Layout_weight to 1, 2, and 3, the following effect appears:
The third one directly does not show, why? Let's take a look at the above method to calculate it:
The system assigns 3 TextView to their desired width fill_parent, which means that each is filled with his parent control, where the width of the dead screen
Then the remaining space at this time = 1 parent_width-3 parent_width=-2 parent_width (Parent_width refers to the screen width)
Then the actual occupied width of the first textview should be =fill_parent width, i.e. parent_width + the weight of his remaining space than column 1/6 * remaining space size ( -2 parent_width) =2/3parent_width
In the same vein the actual occupied width of the second TextView =parent_width + 2/6* ( -2parent_width) =1/3parent_width;
The actual occupied width of the third TextView =parent_width + 3/6* ( -2parent_width) =0parent_width; so 2:1:0 Billy shows. There's no room for the third one.
Android:layout_weight's deep understanding