The Android:layout_weight property tells LinearLayout how to arrange the sub-components.
We've set the same values for two components, but that doesn't mean they occupy the same width on the screen.
Degree. When deciding on the width of the subassembly view, LinearLayout uses the Layout_width and
The mixed value of the Layout_weight parameter. LinearLayout is a two-step process to set the width of a view.
Activity_main.xml
1<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "Horizontal"6 Tools:context=". Mainactivity " >7 8<CheckBox9 Android:id= "@+id/checkbox1"Ten Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Display" /> from<ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "button" /> the</LinearLayout>
View Code
The first step, LinearLayout view the Layout_width property value (vertical orientation view layout_height
property value). The Layout_width property values for the button and checkbox components are set to Wrap_content, so
They have only enough space to draw themselves, as shown in 1
Figure 1 First step: space size allocation based on Layout_width attribute value
Figure 2 Step two: Additional space based on the 1:1layout_weight attribute value assignment
In the layout, the button and checkbox components have the same value for the Layout_weight property, because
They share the same size of extra space. If you set the weight value of the button component to 2, then
It will get 2/3 extra space, and the checkbox component gets the remaining 1/3, as shown in 3.
Figure 3 additional space allocated based on the 2:1layout_weight attribute value inequality
The weight setting value can also be a floating-point number. For weight setting values, developers have their own usage habits.
In Activity_main.xml, we are using a cocktail recipe style of weight styling
。 Another common setting is to add up to 1.0 or 100 for each component property value. So, in the last example, the button
The weight value of the component should be 0.66 or 66.
What do you do if you want LinearLayout to assign exactly the same width to the respective view? Very simple
Just set the Layout_width property value of each component to 0DP to avoid space allocation on the first step, so
LinearLayout will only consider using the Layout_weight attribute value to complete the required space allocation, 4
is shown.
Figure 4layout_width= "0DP", based on 1:1layout_weight attribute value
How the Android:layout_weight property works