First declare that the property is valid only in LinearLayout. The reason why Android:layout_weight is controversial is that while setting this property, setting Android:layout_width to Wrap_content and match_parent can result in two diametrically opposed effects. As shown below:
<LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:background= "@android: Color/black"Android:text= "111"android:textsize= "20SP" /> <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_weight= "2"Android:background= "@android: Color/holo_green_light"Android:text= "222"android:textsize= "20SP" />
The above layout sets the width of the two textview to Match_parent, a weight of 1, and a weight of 2. The results are as follows:
You can see that the weight is 1, but it accounts for Two-thirds!
Let's look at the following layout:
<LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:background= "@android: Color/black"Android:text= "111"android:textsize= "20SP" /> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "2"Android:background= "@android: Color/holo_green_light"Android:text= "222"android:textsize= "20SP" /></LinearLayout>
The width is wrap_content and the view is as follows:
The left TextView accounted for One-third, and normal again.
The true meaning of android:layout_weight is that once the view has this property set (assuming it is valid), the view width is equal to the original width (android:layout_width) plus the amount of space remaining!
Set the screen width to L, the width of the two view is Match_parent, the original width is L, two of the view width is L, then the remaining width is L (l+l) =-L, the left view accounted for One-third, so the total width is L + (-l) *1/3 = (2/ 3) L. In fact, the default view of weight This value is 0, once set this value, then the view at the time of drawing execution onmeasure two times the reason is here.
Google's official recommendation, when using the weight attribute, set the width to 0dip, the effect is the same as set to Wrap_content. So weight can be understood as a proportion!
Reference:
[1] The true meaning of android:layout_weight. http://blog.csdn.net/yanzi1225627/article/details/24667299
Android Development (29) meaning of--layout_weight