Understanding of Android Layout_weight
Explanations in the SDK:
Indicates how much of the extra space in the linearlayout would be allocated to the view associated with these Lay Outparams. Specify 0 if the view should not being stretched. Otherwise the extra pixels would be a pro-rated among all views whose weight is greater than 0.
There are two priorities.
- Layout_weight represents the division of additional space in the LinearLayout (it may also be smaller before the application Layout_weight is expanded).
- Proportional (ratio of layout_weight size)
The following is an example of android:orientation= "horizontal"
Look at the source code, although not very understand, but understand the next general meaning, in accordance with their own understanding summed up, directly write a simplified code bar (the following code is LinearLayout source file part of the simplification, variable name meaning may not be accurate, for the narrative convenient for this explanation):
//Either expand children with weight to take up available space or//Shrink them if they extend beyond our current boundsint 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) {Continuefinal linearlayout.layoutparams LP = ( Linearlayout.layoutparams) Child.getlayoutparams (); float childextra = Lp.weight; if (Childextra > 0int) (Childextra * Delta/ WeightSum); WeightSum-= Childextra; Delta-= share; int childwidth = Child.getmeasuredwidth () + share; if (Childwidth < 0) {childwidth = 0
Variable meaning
- Width of the Widthsize:linearlayout
- Mtotallength: The width of all sub-view and (also no consideration layout_weight)
- Totalweight: Layout_weight of all sub-view and
- The Android:weightsum property of Mweihtsum:linearlayout
Process Analysis:
The extra space is calculated first (can be negative) if the extra space is not 0 and the layout_weight with the child view is not 0, then the extra space is allocated by layout_weight:
int delta = widthsize-mtotallength;
if (Delta! = 0 && totalweight > 0.0f) {
...
}
If LinearLayout set Weightsum, the and of the layout_weight of the child view is overwritten:
float WeightSum = mweightsum > 0.0f? Mweightsum:totalweight;
Then traverse the LinearLayout child element, if it is not null and visibility is not gone, obtain its layoutparams if its layout_weight is greater than 0, Calculates the extra space allocated to it based on the weightsum and its weight
if (Childextra > 0) {
int share = (int) (Childextra * delta/weightsum);
WeightSum-= Childextra;
Delta-= share;
int childwidth = child.getmeasuredwidth () + share;
if (Childwidth < 0) {
childwidth = 0;
}
}
There is an explanation on the Internet that layout_weight indicates the importance of dividing the priority of additional space, which can be known by code as wrong. Layout_weight represents the proportion of division, as for when View Layout_width is Fill_ The problem of the inverse ratio of layout_weight to the parent is explained in my understanding as follows:
For example, the following XML:
<?XML version= "1.0" encoding= "Utf-8"?>
<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
Android:layout_height= "Wrap_content"
Android:background= "#00ff00"
Android:weightsum= "0"
Android:orientation= "Horizontal">
<Button
Android:id= "@+id/imageviewloginstate"
Android:layout_width= "Fill_parent"
Android:layout_height= "Fill_parent"
Android:layout_weight= "1"
Android:text= "1">
</Button>
<Button
Android:id= "@+id/imageviewloginstate1"
Android:layout_width= "Fill_parent"
Android:layout_height= "Fill_parent"
Android:layout_weight= "1"
Android:text= "2">
</Button>
android:id= "@+id/imageviewloginstate2" Span style= "color: #ff0000;" >
Android:layout_width= "fill_parent"
Android : Layout_height= "fill_parent"
Android:layout_ Weight= "2"
Android:text = "3" >
</< Span style= "color: #800000;" >button>
</ linearlayout>
According to general understanding, the ratio of 3 buttons should be 1:1:2, but the reality is this:
As I understand it, the system sets the size of the button, and the variable name is the meaning of the preceding code:
Assume that container is the width of the linearlayout parent_width
The width of the three buttons is fill_parent, so three buttons have a width of parent_width before applying layout_weight
So extra space:
Delta = parent_width-3 * Parent_width =-2 * PARENT
Because LinearLayout is not set Android:weightsum (default is 0, set to 0 is not set), so mweightsum = 1 + 1 +2 =4
So:
The first button has a width of
Parent_width + share = parent_width + (layout_weight * delta/mweightsum)
= Parent_width + (1 * ( -2 * parent_width)/4)
= *parent_width
Then update the weightsum and Delta:
WeightSum-= Childextra; (=3) delta- = share; (=-3/2 * parent_width)
The width of the second button is:
Parent_width + share = parent_width + (layout_weight * delta/mweightsum)
= Parent_width + (1 * ( -3/2 * parent_width)/3)
= *parent_width
Update Weightsum and Delta:
WeightSum-= Childextra; (=2) delta- = share; (=-parent_width)
The width of the third button is:
Parent_width + share = parent_width + (layout_weight * delta/mweightsum)
= Parent_width + (2 * (-Parent_width)/2)
= 0
So the final thing is that the first two buttons split LinearLayout, and the third button disappears.
The approximate process is this, but not all, for example, if the LinearLayout weightsum in the example above is set to 2, the first two buttons have a width of 0, but when the width of the third button is calculated mweightsum = 0, but layout_weight * Delta/ Mweightsum can not calculate, do not know how the system to deal with, in my ability, weightsum for 2 o'clock:
When Weightsum is 3:
As explained in the SDK, layout_weight indicates how extra space is divided, to note the extra 2 words, to have extra space to allocate it proportionally to the child view set Layout_weight, so if LinearLayout is set to WRAP_ Content words are no extra space, layout_weight is useless, as long as the layout_width is not set to wrap_content on the line, can also be set to a specific value, if the value is too small, the extra space is negative, may compress the child control, Make it smaller than the size defined in the XML file, for example:
<?XML version= "1.0" encoding= "Utf-8"?>
<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "100DP"
Android:layout_height= "Wrap_content"
Android:background= "#00ff00"
Android:orientation= "Horizontal">
<Button
Android:id= "@+id/button1"
Android:layout_width= "60DP"
Android:layout_height= "Fill_parent"
Android:layout_weight= "1"
Android:text= "1">
</Button>
<Button
Android:id= "@+id/button2"
Android:layout_width= "60DP"
Android:layout_height= "Fill_parent"
Android:layout_weight= "1"
Android:text= "2">
</Button>
android:id= "@+id/button3"
Android:layout_width= "60DP"
Android:layout _height= "fill_parent"
Android:layout_weight< Span style= "color: #0000ff;" >= "2"
Android:text= "3" >
</button< Span style= "color: #0000ff;" >>
</linearlayout >
Extra Space:
Delta = 100-3 * 60 = 80
Mweightsum = 1 + 1 +2 =4
So:
The width of the first button is:
60+ share = + (Layout_weight * delta/mweightsum)
= 60 + (1 * (-80)/4) = 40
And then:
WeightSum-= Childextra; (=3) delta- = share; (=-60)
The width of the second button is:
+ share = + (Layout_weight * delta/mweightsum)
= 60 + (2 * (-60)/3)
= 40
And then:
WeightSum-= Childextra; (=2) delta- = share; (=-40)
The width of the third button is:
+ share = + (Layout_weight * delta/mweightsum)
= 60 + (2 * (-40)/2)
= 20
:
The following code also shows that layout_weight represents the allocation of additional space:
<?XML version= "1.0" encoding= "Utf-8"?>
<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "200DP"
Android:layout_height= "Wrap_content"
Android:background= "#00ff00"
Android:orientation= "Horizontal">
<Button
Android:id= "@+id/button1"
Android:layout_width= "60DP"
Android:layout_height= "Fill_parent"
Android:layout_weight= "1"
Android:text= "1">
</Button>
android:id= "@+id/button2"
Android:layout_width= "40DP"
Android:layout _height= "fill_parent"
Android:layout_weight< Span style= "color: #0000ff;" >= "1"
Android:text= "2" >
</button< Span style= "color: #0000ff;" >>
</linearlayout>
The extra space is 100, so the width of the Button1 is 60+100/2=110,button2 40+100/2=90.
"Turn" in Android Layout_weight understanding