Android technology 14: layout_weight attribute parsing in Android,
For better space layout, layout_weight is used in LinearLayout. For this attribute, beginners in some books or Android directly think that the greater the value of layout_weight, the greater the control weight, the larger the space occupied, or the smaller the value of layout_wight, the larger the control space. Both are one-sided and do not really realize the meaning of layout_weight and how to layout it. The following describes why the code is used.
1. Demonstration weight is inversely proportional
Set the horizontal layout of LinearLayout, and set the space width to fill_parent. The property ratio of layout_weight to. The effect is as follows:
1 <LinearLayout xmlns: 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 <TextView 7 android: background = "# ff0000" 8 android: layout_width = "fill_parent" 9 android: layout_height = "fill_parent" 10 android: layout_weight = "1" 11 android: text = "red background" 12 android: textColor = "# ffffff"/> 13 <TextView 14 android: background = "#00ff00" 15 android: layout_width = "fill_parent" 16 android: layout_height = "fill_parent" 17 android: layout_weight = "3" 18 android: text = "green background" 19 android: textColor = "# ffffff"/> 20 21 </LinearLayout>
2. proportional to the demo weight
Set the horizontal layout of LinearLayout and set the space width to 0 dip. The layout_weight attribute ratio is. The effect is as follows:
1 <LinearLayout xmlns: 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 <TextView 7 android: background = "# ff0000" 8 android: layout_width = "0dip" 9 android: layout_height = "fill_parent" 10 android: layout_weight = "1" 11 android: text = "red background" 12 android: textColor = "# ffffff"/> 13 <TextView 14 android: background = "#00ff00" 15 android: layout_width = "0dip" 16 android: layout_height = "fill_parent" 17 android: layout_weight = "3" 18 android: text = "green background" 19 android: textColor = "# ffffff"/> 20 21 </LinearLayout>
3. Layout_wight layout Calculation
This statement is incorrect regardless of whether the space size is proportional to the weight or inverse ratio. In Android, the system calculates the space size. First, layout the space based on the size of layout_width, and then allocate the space according to the weight ratio set by layout_weight. If no value is set, the default value is 0.
Space occupied by the control = layout_width space + (available space) * The remaining space of the Space's weight can be positive or negative.
The remaining space is the remaining space after the space is filled. When the layout_width attribute of all controls is fill_parent (full screen), the excess space is the remaining space, the value is a negative value. The number of examples is used below. For the three textviews in LinearLayout, layout_width = "fill_parent" layout_weight ratio is.
One fill_parent = Full Screen X. The LinearLayout child control will be measure twice. For the first time, 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, beyond the 2 full screen, textview1 weight 1/6, textview2 weight 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 calculation above, textview1 accounts for 2/3 of the screen, textview2 accounts for 1/3 of the screen, and textview3 does not display.
1 <LinearLayout xmlns: 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 <TextView 7 android: background = "# ff0000" 8 android: layout_width = "fill_parent" 9 android: layout_height = "fill_parent" 10 android: layout_weight = "1" 11 android: text = "red background" 12 android: textColor = "# ffffff"/> 13 <TextView 14 android: background = "#00ff00" 15 android: layout_width = "fill_parent" 16 android: layout_height = "fill_parent" 17 android: layout_weight = "2" 18 android: text = "green background" 19 android: textColor = "# ffffff"/> 20 <TextView 21 android: background = "# 0000ff" 22 android: layout_width = "fill_parent" 23 android: layout_height = "fill_parent" 24 android: layout_weight = "3" 25 android: text = "blue background" 26 android: textColor = "# ffffff"/> 27 </LinearLayout>
The sub-element attribute android: layout_weight in LinearLayout has other features.
I don't know what you are asking. I currently use this property. For example, one row has two controls, and each control
Android: layout_weight = "1" and they will divide the region equally.
For example, if you have a TextView, the following ListView only needs to set android: layout_weight = "1" for the listview to make the textview not covered during the listview drop-down.
In Android, how does view set the layout_weight attribute through code?
It can be changed in the code, but it cannot be obtained through the getWidth method. layout should be obtained.
Class, directly retrieve the Width attribute.