Mixed use of weightsum and Layout_weight
Look at the effect first, the button occupies half the width of the screen.
Look again at the description in the development documentation.
"Defines the maximum value for 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. A typical case is: by specifying the Layout_weight property of the child view to 0.5, and setting the LinearLayout's Weightsum property to 1.0, the implementation child view occupies 50 of the available width. ”
The source code of the XML file.
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:background=" #ffffff " android:gravity=" center " android:orientation= "Horizontal" android:weightsum= "1" > <button android:layout_width= " 0DP " android:layout_height=" wrap_content " android:layout_weight=" 0.5 " android:text=" @string/ Activity_main_click_me "/></linearlayout>
The Android:layout_width property of the specified button is 0DP, so the width of the button needs to be determined based on the Android:weightsum property.
Assume that there is a width of 200dp,android:weightsum property that is 1.0 linearlayout. The formula for the button width in this linearlayout is as follows.
Button ' s width + button ' s weight * 200/sum (weight)
Specifies that the button has a width of 0dp,weight of 0.5,sum (weight) equal to 1, then the result is as follows.
0 + 0.5 * 200/1 = 100
Profile
Using the LinearLayout weight property is necessary when you need to allocate the available space on a proportional basis, which avoids the side effects of hard-coded methods. If the target platform is honeycomb and uses fragment, then most of the cases are using weight to go to the layout file and give you a space for the fragment allocation. An in-depth understanding of how to use weight will add an important skill to the developer.
External links
Http://developer.android.com/reference/android/widget/LinearLayout.html
Http://mobile.51cto.com/abased-375428.htm
"Android Development Note" Weight properties