Explanation: The Layout_weight parameter is an integer value whose value is used to specify the allocation scale of the parent control's free space.
The following examples illustrate
The red part is the free space
Related code:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" android:b ackground= "#ff0000" > <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap _content "android:orientation=" horizontal "> <textview android:id=" @+id/textview1 " Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "#00f F00 "android:textsize=" 30SP "android:text=" first "/> <textview android:id=" @ +id/textview2 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "a Ndroid:background= "#ffff00" android:textsize= "30SP" android:text= "second"/> </linearlayout ></linearlayout>
Set the Layout_weight property of two TextView to 1
, first and second are full of parent control controls, and Layout_weight are set to 1 in the sense that:
The average free space is divided into 2 parts, first and second each, not the entire parent control into 2 parts
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:background= "#ff0000" android:orientation= "vertical" > <linearl Ayout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= "H Orizontal "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:background= "#00ff00" android:text= "first" Android:textsize= "30sp"/> <textview android:id= "@+id/textview2" android:layou T_width= "Wrap_content" android:layout_height= "wrap_content" android:background= "#ffff00" android:text= "Second" android:textsize= "30sp"/> </LinearLayout> <linearlayout Android : Layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_margintop= "3DP" android:orientation= "Horizontal" > <textview android:id= "@+id/textview3" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "#00ff00" android:text= "First" android:textsize= "30sp"/> <textview android:id= "@+id/textview 4 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layou t_weight= "1" android:background= "#ffff00" android:text= "second" android:textsize= "30SP"/ > </LinearLayout></LinearLayout>
finally share a tip, if you want first to account for One-third of the parent control, second accounts for Two-thirds of the parent control
Set the width of the control to 0,layout_weight to the appropriate scale
<linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android: layout_margintop= "3DP" android:orientation= "Horizontal" > <textview android:id= "@+id/ TEXTVIEW3 " android:layout_width=" 0DP " android:layout_height=" wrap_content " android:layout_weight=" 1 " android:background=" #00ff00 " android:text=" first " android:textsize=" 30sp "/> < TextView android:id= "@+id/textview4" android:layout_width= "0DP" android:layout_height= "Wrap_ Content " android:layout_weight=" 2 " android:background=" #ffff00 " android:text=" second " Android:textsize= "30sp"/> </LinearLayout>
Introduction to the use of Android layout_weight properties