Layout_weight attribute in linearlayout
Layout linearlayout code
XML 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"
- >
- <Button
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "button1"
- Android: Background = "#008000"
- Android: layout_weight = "0"
- />
- <Button
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: text = "button2"
- Android: Background = "# FFFF00"
- Android: layout_weight = "0"
- />
- </Linearlayout>
<? 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"> <button Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "button1" Android: Background = "#008000" Android: layout_weight = "0"/> <button Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "button2" Android: Background = "# FFFF00" Android: layout_weight = "0"/> </linearlayout>
If the layout_weight attribute is not set, the default value is 0.
Button 1: button1, green
Button 2: button2, yellow
The following is a record of the changes in the weight attribute (layout_weight) of Button 1 and button 2.
The vertical layout Android: Orientation = "vertical" is tested here. Therefore, the Android: layout_height attribute of the component is used. If the horizontal layout is used, the Android: layout_width attribute of the component is used.
(1) Android: layout_height = "fill_parent"
|
|
Button1 (Weight = 0) button2 (Weight = 0) |
Button1 (Weight = 0) button2 (Weight = 1) |
|
|
Button1 (Weight = 1) button2 (Weight = 1) Button1 accounts for 1/2, while button2 accounts for 1/2 |
Button1 (Weight = 1) button2 (Weight = 2) Button1 accounts for 2/3, while button2 accounts for 1/3 |
|
|
Button1 (Weight = 1) button2 (Weight = 9) Button1 accounts for 9/10, while button2 accounts for 1/10 |
Button1 (Weight = 1) button2 (Weight = 30) Button1 accounts for 30/31, while button2 accounts for 1/31 |
|
|
(2) Android: layout_height = "wrap_content"
|
|
Button1 (Weight = 0) button2 (Weight = 0) |
Button1 (Weight = 0) button2 (Weight = 1) |
|
|
Button1 (Weight = 1) button2 (Weight = 1) Button1 accounts for 1/2, while button2 accounts for 1/2 |
Button1 (Weight = 1) button2 (Weight = 2) Button1 accounts for 1/3, while button2 accounts for 2/3 |
|
|
Button1 (Weight = 1) button2 (Weight = 9) |
Button1 (Weight = 1) button2 (Weight = 30) |
|
|
|
|
The above experiments show that:
(1) Android: layout_height = "fill_parent"
(2) Android: layout_height = "wrap_content"
In the case of (1) and (2), the opposite is displayed under the same weight attribute.