When the control itself layout_width set to Fill_parent, the smaller the layout_weight value, the larger the space, but the limit is as large as possible, that is, fill_parent.
When the control itself layout_width set to Wrap_content, the smaller the Layout_weight value, the smaller the space, but this small is limited, that is, wrap_content.
Example
Look at the source code, although not quite understand, but understand the next general meaning, according to their own understanding of the summary, directly to write a simplified code it (the following code is a part of the LinearLayout source file simplification, variable name may not be accurate, for the description of convenient temporarily explain):
The code is as follows |
Copy Code |
Either expand children with weight to take up available spaces or Shrink them if they extend beyond our current bounds int delta = widthsize-mtotallength; if (Delta!= 0 && totalweight > 0.0f) { float weightsum = mweightsum > 0.0f? Mweightsum:totalweight; for (int i = 0; i < count; ++i) { Final View child = Getvirtualchildat (i); if (child = null | | child.getvisibility () = View.gone) { continue; } Final Linearlayout.layoutparams LP = (Linearlayout.layoutparams) child.getlayoutparams (); float Childextra = lp.weight; if (Childextra > 0) { int share = (int) (Childextra * delta/weightsum); Weightsum-= Childextra; Delta-= share; int childwidth = child.getmeasuredwidth () + share; if (Childwidth < 0) { childwidth = 0; } } } } |
Variable meaning
Width of widthsize:linearlayout
Mtotallength: The width of all the child view (not yet considered layout_weight)
Totalweight: The layout_weight of all child view and
Android:weightsum Properties of Mweihtsum:linearlayout
Process Analysis:
First calculate the extra space (can be negative) allocate extra space by layout_weight if the extra space is not 0 and the layout_weight with the child view is not 0:
The code is as follows |
Copy Code |
int delta = widthsize-mtotallength; if (Delta!= 0 && totalweight > 0.0f) { ... } |
If LinearLayout sets the weightsum, it overwrites the layout_weight of the Child view:
The code is as follows |
Copy Code |
float weightsum = mweightsum > 0.0f? Mweightsum:totalweight; |
It then iterates through the linearlayout, if it is not null and visibility is not gone, gets its layoutparams if its layout_weight is greater than 0, Calculate the extra space allocated to it according to the Weightsum and its weight
The code is as follows |
Copy Code |
if (Childextra > 0) { int share = (int) (Childextra * delta/weightsum); Weightsum-= Childextra; Delta-= share; int childwidth = child.getmeasuredwidth () + share; if (Childwidth < 0) { childwidth = 0; } } |
Example
The code is as follows |
Copy Code |
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "Vertical" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > <linearlayout android:orientation= "Horizontal" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" > <textview android:text= "Redwwwwwww" Android:gravity= "Center_horizontal" Android:background= "#aa0000" Android:layout_width= "Wrap_content" android:layout_height= "Fill_parent" android:layout_weight= "1"/> <textview android:text= "Green" Android:gravity= "Center_horizontal" Android:background= "#00aa00" Android:layout_width= "Wrap_content" android:layout_height= "Fill_parent" android:layout_weight= "2"/> <textview Android:text= "Blue." Android:gravity= "Center_horizontal" Android:background= "#0000aa" Android:layout_width= "Wrap_content" android:layout_height= "Fill_parent" android:layout_weight= "3"/> <textview android:text= "Yellow" Android:gravity= "Center_horizontal" Android:background= "#aaaa00" Android:layout_width= "Wrap_content" android:layout_height= "Fill_parent" android:layout_weight= "4"/> </LinearLayout> </LinearLayout> |