In order to better layout the space, using Layout_weight in LinearLayout, and then for this attribute, in some books or Android beginners directly think that the greater the Layout_weight value, the greater the weight of the control, The larger the space occupied or the smaller the Layout_wight value, the greater the control space. Both are one-sided and do not really recognize the meaning of layout_weight and how to lay it out. The following first shows why the use of code has these two senses.
1. The presentation weights are inversely proportional
LinearLayout set the horizontal layout, and then the space width of the Fill_parent,layout_weight property ratio of 3:1, the effect is as follows
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "Horizontal">6 <TextView7 Android:background= "#ff0000"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Fill_parent"Ten Android:layout_weight= "1" One Android:text= "Red Background" A Android:textcolor= "#ffffff"/> - <TextView - Android:background= "#00ff00" the Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" - Android:layout_weight= "3" - Android:text= "Green background" + Android:textcolor= "#ffffff"/> - + </LinearLayout>
2. Proportional to the presentation weight
LinearLayout set the horizontal layout, and then the space width of the 0dip,layout_weight property ratio of 3:1, the effect is as follows
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "Horizontal">6 <TextView7 Android:background= "#ff0000"8 Android:layout_width= "0dip"9 Android:layout_height= "Fill_parent"Ten Android:layout_weight= "1" One Android:text= "Red Background" A Android:textcolor= "#ffffff"/> - <TextView - Android:background= "#00ff00" the Android:layout_width= "0dip" - Android:layout_height= "Fill_parent" - Android:layout_weight= "3" - Android:text= "Green background" + Android:textcolor= "#ffffff"/> - + </LinearLayout>
3.layout_wight Layout Calculation
Whether the size of the space is proportional to the weight or the inverse is not true, the system in Android has a way of calculating the size of the space. First, according to the size of the layout_width layout space, and then the remaining space according to Layout_weight set the weight ratio allocation space, if not set, the default is 0.
Control occupies space size =layout_width space + (remaining space) * The space weight weight remaining space can be positive negative.
The rest of the space here, that is, when there is space left behind, when all controls Layout_width property is fill_parent (screen fullscreen), then the excess portion is the remaining space, but the value is negative. Here is the number of specific examples. LinearLayout within 3 textview,layout_width= "Fill_parent" layout_weight than 1:2:3.
A fill_parent= full screen x,linearlayout layout child control measure two times, the first time, respectively, layout the size of each textview space, that is TEXTVIEW1=X,TEXTVIEW2=X,TEXTVIEW3 =x, the second allocation of the remaining space, the remaining space size =x-3x=-2x, that is, out of 2 full screen, textview1 weight 1/6,textview2 weight value of 2/6,textview3 weight 3/6, so
textview1=x+ ( -2x) *1/6=2/3x
textview2=x+ ( -2x) *2/6=1/3x
textview3=x+ ( -2x) *3/6=0
According to the above calculation get textview1 accounted for 2/3 screen, textview2 accounted for 1/3 screen, textview3 not display.
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Fill_parent"4 Android:layout_height= "Fill_parent"5 android:orientation= "Horizontal">6 <TextView7 Android:background= "#ff0000"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Fill_parent"Ten Android:layout_weight= "1" One Android:text= "Red Background" A Android:textcolor= "#ffffff"/> - <TextView - Android:background= "#00ff00" the Android:layout_width= "Fill_parent" - Android:layout_height= "Fill_parent" - Android:layout_weight= "2" - Android:text= "Green background" + Android:textcolor= "#ffffff"/> - <TextView + Android:background= "#0000ff" A Android:layout_width= "Fill_parent" at Android:layout_height= "Fill_parent" - Android:layout_weight= "3" - Android:text= "Blue background" - Android:textcolor= "#ffffff"/> - </LinearLayout>