Let's take a look at the function description of the Weightsum property: Defines the maximum value of the weight sum. If this value is not specified, the cumulative value of the Layout_weight property of all child views is used as the maximum value of the sum. Put the definition of weightsum here, first to see how android:layout_weight use.
Android:layout_weight is the weight that is used to assign the remaining width to LinearLayout's child controls, and the width of each child control is calculated as: view width = view original width + ((linearlayout width- (Sum of all child view widths)/weight max) Xview weight value. According to my formula, calculate the sub-view width in the following linearlayout:
We assume that the screen width is 480, assuming that the wrap_content width of the TextView is 20
TextView1 width = + ((480-(20X3))/2) x0 = 20
TextView2 width = + ((480-(20X3))/2) x1 = 230
TEXTVIEW3 width = + ((480-(20X3))/2) x1 = 230
The final display looks like this:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android : orientation= "Horizontal" > <textview android:layout_width= "wrap_content" android:layout_he ight= "Wrap_content" android:background= "#0045f5" android:gravity= "center" android:text= "1"/> ; <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:b Ackground= "#00ff47" android:gravity= "center" android:text= "2" android:layout_weight= "1"/> <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and Roid:background= "#ff5600" android:gravity= "center" android:layout_weight= "1" android:text= "3"/ > </LinearLayout>
So by understanding the formula given above, we can see that if we set the width of the sub-view to 0DP, we would be able to assign the width of the child view by weight. Conversely, when we set the child view width to wrap_content, the width of the control is affected by two factors such as weight and the width of the view itself. When the child view width is set to fill_parent, the effect of the pair view is more pronounced, such as the following code:
We assume that the screen width is 480, and TextView because the width is set to Fill_parent, so the width is also 480
TextView1 width = 480 + ((480-(480X3))/6) X1 = 320
TextView2 width = 480 + ((480-(480X3))/6) x2 = 160
TEXTVIEW3 width = 480 + ((480-(480X3))/6) x3 = 0
So the result is that TextView1 accounted for the 2/3,textview2 accounted for 1/3,TEXTVIEW3 no display space. In fact, regardless of the width of the set, as long as the formula calculation, we can understand why the final display effect.
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android : orientation= "Horizontal" > <textview android:layout_width= "fill_parent" Android:layout_hei ght= "Wrap_content" android:background= "#0045f5" android:gravity= "center" android:layout_weight= "1" android:text= "1"/> <textview android:layout_width= "Fill_parent" android:layou t_height= "Wrap_content" android:background= "#00ff47" android:gravity= "center" android:text= "2" android:layout_weight= "2"/> <textview android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:background= "#ff5600" android:gravity= "center" Android:layo ut_weight= "3" android:text= "3"/> </LinearLayout>
Finally say weightsum, this property is set on the LinearLayout, just like the beginning is to set the maximum value of weight sum. How do you understand it? For example, we want 3 TextView to be displayed as 1:2:2, which needs to be set as shown in the code below. At this point the weightsum is actually 1+2+2=5, that is, if the value of LinearLayout is not specified, the cumulative value of the Layout_weight attribute of all the child views as the maximum value of the sum. Then if I set weightsum manually, the weight of the child view must be assigned by Weightsum. For example, if we set Weightsum to 1, and TextView1 's weight set to 0.2,textview2 weight setting to 0.4,TEXTVIEW3 is set to 0.4, the same effect can be achieved 1:2:2. So Weightsum's role is defined as: "Defines the maximum value of the sum of weight. If this value is not specified, the cumulative value of the Layout_weight property of all child views is used as the maximum value of the sum. The
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android : orientation= "Horizontal" > <textview android:layout_width= "wrap_content" android:layout_he ight= "0DP" android:background= "#0045f5" android:gravity= "center" android:text= "1" Androi d:layout_weight= "1"/> <textview android:layout_width= "Wrap_content" android:layout_height= "0DP" android:background= "#00ff47" android:gravity= "center" android:text= "2" Android: layout_weight= "2"/> <textview android:layout_width= "wrap_content" android:layout_height= "0d P "android:background=" #ff5600 "android:gravity=" center "android:layout_weight=" 2 "and" roid:text= "3"/> </LinearLayout>
In fact, it is relatively simple to write, mainly to make a note for themselves, the key to write a little bit. If you have little help, please forgive me!
Reference connection:
http://blog.csdn.net/xiaanming/article/details/13630837
Http://book.51cto.com/art/201404/435840.htm
Reference Code: Http://files.cnblogs.com/files/endure/ListViewTable.rar (Demo version in reference connection 1)
Android Layout_weight and Weightsum