Use weight to control two buttons in horizontal direction 1:2
Horizontal direction: Layout_width = "0DP"
Using the Weight property
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Button1" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "0DP"Android:layout_weight= "2"Android:layout_height= "Wrap_content"Android:text= "Button2" /></LinearLayout>
Change slightly: Layout_width = "Match_parent"
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Button1" /> <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_weight= "2"Android:layout_height= "Wrap_content"Android:text= "Button2" /></LinearLayout>
Comparison of the two effects
Only changes the layout value, resulting in two different display types.
How the actual width of the control is calculated:
width = percent of original width + remaining width;
In layman's terms:
width = layout_weight defined width + (screen width-width of two components layout_weight) * Percentage
Assuming the screen width is L
for layout_width = "0DP "
Button1: = 0DP + (L-0DP-0DP) * 1/3 = 1/3l
Button2: = 0DP + (L-0DP-0DP) * 2/3 = 2/3l
Layout_width= "Match_parent", the original width is L
Button1 = L + (L-(l+l)) *1/3 = l-1/3l = 2/3l
Button2 = L + (L-(l+l)) *2/3 = l-2/3l = 1/3l
Android screen adaptation guide (2) weight attention.