This article address: http://www.cnblogs.com/wuyudong/p/5898403.html, reprint please indicate source address.
If you want to implement the center display of the button as shown, and occupy half of the parent view, whether the screen is rotated or not
Pooled Weightsum properties and Layout_weight properties
The sizes of different Android devices are often different. As a developer, we need to create an XML file for screens of different sizes. Hard coding is undesirable, so other methods are needed to organize the view.
This article analyzes how to share the two properties of Layout_weight and weightsum to fill any remaining space inside the layout. A description of Android:weightsum's development documentation is similar to what we now want to implement, with the following 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. ”
Imagine a scenario where we want to place other objects in the box. The ratio of free space in the box is weightsum, and the proportion of free space in the box is layout_weight. For example, the weightsum of a box is 1, and we need to place two objects in the box: Object A and Object B. The layout_weight of object A is 0.25, and the Layout_weight of object B is 0.75. So, object A can occupy 25% of the space in the box, and object B can occupy the remaining 75% of the space.
We specify a weightsum for the parent view, and then specify that the button's Android:layout_weight property is half of Weightsum. The source code for the XML file is as follows:
<LinearLayoutxmlns: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" > <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content" android:layout_weight= "0.5" Android:text= "click me" /></LinearLayout>
It is necessary to use the weight property of linearlayout when developers 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, the reader will find that most of the cases use weight to allocate space for fragment in the layout file. An in-depth understanding of how to use weight will add an important skill to the reader.
Android Hack1 using the Weight property to center the view