These two days when making an Android interface with the LinearLayout linear layout, and placed the TextView control in it, set the Android:layout_width property to Wrap_content, Eclipse hints that the use of 0DP instead of wrap_content can get a better experience, suddenly become curious, why use 0DP is better? So explored a few, on-line has related articles, after learning to make a summary.
First explain the role of the Android:layout_weight attribute, in fact, simple understanding is used to allocate space size, as to how to allocate, the allocation of what space, please continue to see ...
When we use the LinearLayout linear layout, place three textview spaces, set the Android:layout_width property to Wrap_content, and set the Android:layout_weight gravity to 1 respectively, 2, 3 o'clock:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Fill_parent"3 Android:layout_height= "Fill_parent" >4 5 <TextView6 Android:id= "@+id/textview1"7 Android:layout_width= "Wrap_content"8 Android:layout_height= "Wrap_content"9 Android:layout_weight= "1"Ten Android:background= "#990033" One Android:text= "1" /> A - <TextView - Android:id= "@+id/textview2" the Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:layout_weight= "2" - Android:background= "#339933" + Android:text= "2" /> - + <TextView A Android:id= "@+id/textview3" at Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:layout_weight= "3" - Android:background= "#0000CC" - Android:text= "3" /> - in </LinearLayout>
View Code
UI Effects:
You can see that the width ratio of the three TextView is 1:2:3, which seems to achieve proportional control according to the proportion of the android:layout_width we set, however, when we try to change the text content of Textview1 to 111111111,
1 <TextView2 Android:id= "@+id/textview1"3 Android:layout_width= "Wrap_content"4 Android:layout_height= "Wrap_content"5 Android:layout_weight= "1"6 Android:background= "#990033"7 Android:text= "111111111" />
View Code
The UI effect will appear:
The original proportional balance was broken, and now the width of the three TextView is no longer 1:2:3. Why is it? Because the Layout_width property does not allocate the entire free space, it allocates the rest of the space, that is, the system allocates space first by the properties set by Layout_width, where the code first allocates space so that each control is sufficient to wrap the contents. Then the remaining content is allocated according to the proportions set by Layout_weight, the final space size of the control is the space size of the Layout_width property, plus the amount of remaining space proportionally, and you will be able to find out whether the UI removes the control outside the content or 1:2:3.
At this point, we follow Eclipse's suggestion to change the properties of Layout_width to 0DP, try:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Fill_parent"3 Android:layout_height= "Fill_parent" >4 5 <TextView6 Android:id= "@+id/textview1"7 Android:layout_width= "0DP"8 Android:layout_height= "Wrap_content"9 Android:layout_weight= "1"Ten Android:background= "#990033" One Android:text= "111111111" /> A - <TextView - Android:id= "@+id/textview2" the Android:layout_width= "0DP" - Android:layout_height= "Wrap_content" - Android:layout_weight= "2" - Android:background= "#339933" + Android:text= "2" /> - + <TextView A Android:id= "@+id/textview3" at Android:layout_width= "0DP" - Android:layout_height= "Wrap_content" - Android:layout_weight= "3" - Android:background= "#0000CC" - Android:text= "3" /> - in </LinearLayout>
View Code
The UI effect at this point is:
At this point we find that the width ratio of the three TextView is the ratio of the Layout_weight attribute we set, why? I believe you will forget, the rest of the space =parent space-layout_width set space size, where we are set to 0, so the remaining space is the parent space, and the content is irrelevant.
Let's look at a better game, set Layout_width to the Match_parent property, and then set the three control scale to 1:2:2:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Fill_parent"3 Android:layout_height= "Fill_parent" >4 5 <TextView6 Android:id= "@+id/textview1"7 Android:layout_width= "Match_parent"8 Android:layout_height= "Wrap_content"9 Android:layout_weight= "1"Ten Android:background= "#990033" One Android:text= "1" /> A - <TextView - Android:id= "@+id/textview2" the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:layout_weight= "2" - Android:background= "#339933" + Android:text= "2" /> - + <TextView A Android:id= "@+id/textview3" at Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:layout_weight= "2" - Android:background= "#0000CC" - Android:text= "3" /> - in </LinearLayout>
View Code
The UI effect at this point should surprise you:
Well, isn't that a proportional control? Why is it that the ratio of textview1 to 1 is greater than that of a 2-percent control?
Let's make a good calculation with the formula we just made:
The remaining space is the size of the property set by the parent control's size, which is 1 parent minus layout_width, and each control's original size is set to match_parent, which means that each control should have the size of the 1 parent control. There are 3 TextView controls, that is, the size of the remaining space = 1 parent-3 parent size =-2 A parent, so the size of Textview1 = The original size 1 parent+ The amount of remaining space allocated is ( -2parent) *1/ 5, the last equals 3/5 parent, and the remaining two controls can be calculated as 1/5 parent, so the scale is 3:1:1.
Reference article: http://blog.csdn.net/xiechengfa/article/details/38334327
Android:layout_weight properties of the Android UI and proportional control of controls