Recently read "Android Hacks", ready to thank reading notes, and constantly plump.
Heard the question, "What should I do if I want a button to account for 50% of the parent control".
In general, we can use the LinearLayout property Android:layout_weight property
There are several ways to implement this approach.
Android devices have different size, for different screen sizes, we should have a common
The applicable method.
We can use the Layout_weight and Weightsum properties to fill the remaining space of the layout.
Where android:weightsum defines the maximum value of a weight. If the Weightsum property
As defined, it is equal to the sum of the weight of all the child controls it contains. For the above question,
You can set Layout_weight to 0.5 for the unique character space in the control, and set Weightsum to 1.0
Imagine that we are going to place items in a box. The free space of the box is Weightsum,
The corresponding Layout_weight attribute indicates the space occupied by the items in the box. To give a simple example,
The entire box has a weightsum of 1, with two controls, A and B, where A is layout_weight
The layout_weight of 0.25,b is 0.75. So control A has 25% of the space, and control B has 75%
Space.
So we go back to the beginning of the problem, we give the weightsum of the parent control, and then give the child control the
The button whose property android:layout_weight takes half its value. Where the XML attributes are
<?xml version= "1.0" encoding= "Utf-8"?>
<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"//Gets the width of the button
android:layout_height= "Wrap_content"
android:layout_weight= "0.5"//occupy 50% of the remaining space
android:text= "click Me"/>
</LinearLayout>
LinearLayout gets the Android:weightsum property and specifies that the sum of weight for all child controls is 1.
Then, because 1,button is the only child control. 2. Set the width to 0DP and then based on the Weigth property 0.5
So the entire button occupies 50% of the parent control.
So if the width of the linearlayout is 200DP, its properties are android:weightsum=1. Then there will be the following formula
Button Width+button weight*200/sum (weight).
Corresponds to a specific instance of 0dp+0.5*200/1=100
Reading notes _ "Android Hacks" one of LinearLayout's weightsum and weights