LinearLayout is very common in Android development, and the android:layout_weight of LinearLayout's internal controls is important in some scenarios, such as the need to scale. Android does not provide a control such as table, although there is a tablelayout, but it is not as we imagine the HTML inside the table so useful, we often use the ListView to achieve table effect, but the column alignment is more troublesome, Now with LinearLayout and attribute android:layout_weight can be well solved. Now let's experience the next Layout_weight property together.
First, the layout_width of the control within the LinearLayout is set to "Wrap_content", look at the XML configuration:
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "1"/>
</LinearLayout>
The effect is as follows:
You can see that these three textview are displayed in proportion to the 1:2:3, so it seems to be possible to achieve a proportional display, but there is a problem, if the length of text within the TextView together with the longer text TextView will increase the width, see the following configuration and effect:
Configuration:
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1111111111111111111111111111111111111111111"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout>
Effect:
So it seems that we need to scale and can not be achieved, through the world Google finally found the solution is to set the Layout_width set to "Wrap_content". Configuration and effect see below:
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1111111111111111111111111111111111111111111"/>
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout>
Effect:
This finally achieves our proportional display effect, it is very strange, Android development framework of the big guys sometimes design is really a little weird.
Second, the layout_width of the control within the LinearLayout is set to "Fill_parent", take a look at the XML configuration:
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
</LinearLayout>
The effect is as follows:
Oddly enough, the entire width is divided by 3, and the first TextView two, so the smaller the weight value, the greater the priority. Only two textview seem to see some truth, so let's see what the three effects are:
<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout>
Effect:
What do you mean? The third TextView lost, it is very strange, let us try again, the weight to 2,3,4 to see the effect:
The effect is confusing, and I've been trying to find an exact proportional formula, but I haven't found it yet. If any of the great gods can handle it, forget to enlighten me.
Although this android:layout_weight attribute is very strange, fortunately we have achieved the goal:
Displays each child control in the LinearLayout proportionally, set android:layout_width= "0DP" If the setting is Android:layout_height= "0DP" in the vertical direction. In this case, the proportion of a child control occupying LinearLayout is the weight of the value/weight value of all controls within the linearlayout of this control.
Transferred from: http://blog.sina.com.cn/s/blog_7cd0c0a80100zmfe.html
Android:layout_weight attribute details (GO)