The Layout_weight property in the Android layout file is only valid in the LinearLayout layout.
Google recommends: When the Layout_weight property of the control is set, the corresponding Layout_width or Layout_height property of the control should be set to 0DP.
If you set the Layout_weight property of a control and set the Layout_width or Layout_height property at the same time, there are a number of things that need to be parsed, (some controls may have the Layout_weight property set, some not set, The Layout_weight property is set in the control, its corresponding layout_width or Layout_height properties may be the same or different, the end of this article will briefly describe a few cases ), programming is for convenience, It's not about asking for trouble (Google does not recommend the behavior, and why bother yourself).
The following example sets the Layout_weight property to a control whose corresponding layout_width or Layout_height property is 0DP.
The following is my understanding of the properties of the layout_weight, if there are inappropriate, please correct me, the following first look at a few effects, all understand
Situation One
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <button android:text= "Button1" android:layout_ weight= "1" android:layout_width= "match_parent" android:layout_height= "0DP"/> </ Linearlayout>
Scenario Two
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "wrap_content"/> <button android:text= "Button1" android:layout_ weight= "0" android:layout_width= "match_parent" android:layout_height= "0DP"/> </ Linearlayout>
Scenario Three
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "match_parent"/> <button android:text= "Button1" android:layout_ weight= "1" android:layout_width= "match_parent" android:layout_height= "0DP"/> </ Linearlayout>
Scenario Four
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "match_parent"/> <button android:text= "Button1" android:layout_ weight= "0" android:layout_width= "match_parent" android:layout_height= "0DP"/> </ Linearlayout>
Scenario Five
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "100DP"/> <button android:text= "Button1" android:layout_weight= "1 " android:layout_width=" match_parent " android:layout_height=" 0DP "/> </LinearLayout>
Scenario Six
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_width= "Match_parent" android:layout_height= "100DP"/> <button android:text= "Button1" android:layout_weight= "0 " android:layout_width=" match_parent " android:layout_height=" 0DP "/> </LinearLayout>
Situation VII
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_weight= "1" android: Layout_width= "Match_parent" android:layout_height= "0DP"/> <button android:text= "Button1" android:layout_weight= "1" android:layout_width= "match_parent" android:layout_height= "0DP"/> </LinearLayout>
Scenario Eight
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:text= "Button2" android:layout_weight= "0" android: Layout_width= "Match_parent" android:layout_height= "0DP"/> <button android:text= "Button1" android:layout_weight= "0" android:layout_width= "match_parent" android:layout_height= "0DP"/> </LinearLayout>
From the above example, it is not difficult to draw the following conclusions:
1. when all controls in the layout file have the Layout_weight property set, the control is allocated according to the size of the layout_weight, and the larger the value, the greater the space allocated, and the smaller the lower the value.
2. when a part of a control in a layout file has the Layout_weight property set, some controls do not have the Layout_weight property set, the control that does not have the Layout_weight property set is assigned the control, and if there is space left on the screen after allocation, Assign the control that has the Layout_weight property set. (Controls that do not have the Layout_weight property set have spatial precedence).
3. If the Layout_weight property value of the space is set to 0, the control is no longer displayed on the screen.
-----------------------------------------------------------------Split Line--------------------------------------- --------------------------
----------------------------------------------------- The following example is a non-recommended----------------------------- ------------------------
Situation One
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:layout_width= "match_parent" android:layout_height= " Match_parent " android:layout_weight=" 0 " android:text=" Button1 "/> <button android:layout_ Width= "Match_parent" android:layout_height= "match_parent" android:layout_weight= "0" android: text= "Button3"/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_ Content " android:text=" Button2 "/> </LinearLayout>
Scenario Two
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:layout_weight=" 0 " android:text=" Button1 "/> <button android:layout_ Width= "Match_parent" android:layout_height= "match_parent" android:layout_weight= "0" android: text= "Button3"/> <button android:layout_width= "match_parent" android:layout_height= "Wrap_ Content " android:text=" Button2 "/> </LinearLayout>
Scenario Three
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:layout_width= "match_parent" android:layout_height= " Match_parent " android:text=" Button2 "/> <button android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:layout_weight= "0" android:text= "Button1"/> < Button android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_ weight= "0" android:text= "Button3"/> </LinearLayout>
Scenario Four: Note that at this point the Button3 has exceeded the screen
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:text=" Button2 "/> <button android:layout_width=" Match_parent " android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "Button1"/> < Button android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_ weight= "0" android:text= "Button3"/> </LinearLayout>
Scenario Five
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <button android:layout_width= "match_parent" android:layout_height= " Wrap_content " android:text=" Button2 "/> <button android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:layout_weight= "0" android:text= "Button1"/> < Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_ weight= "1" android:text= "Button3"/> </LinearLayout>
The above is simply a few examples, not all the cases, from the above example, summed up some of the conclusions
1. Priority to display a control that does not have a Layout_height property value or a property value of 0, if both are present in the order in which they appear (provided the screen has space remaining)
2. If a control with a Layout_weight property value of 0 and a control with a Layout_weight property value of 0 are present, the control with the Layout_weight property value of 0 is preferred, but the order in which the control is in the layout file will also have an effect on the screen display. For example, scenario four and scenario five show that the two positions in the layout file are not the same, although the order of the display is the same, but the case of four kinds of Button3 is beyond the screen range
3. Other more complex situations are not covered.
4. In summary, if the Layout_weight property of the control is set, it is recommended to follow Google's recommendation to set the corresponding Layout_width or Layout_height property value to 0px
Layout_weight properties of Android linearlayout layout