look first for example the following layout:
In The size of the button is half the screen, then centered in the center of the layout, each person has its own answer, see my method, layout layout XML such as the following:
<?XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:id=" @+id/main_layout " android:layout_width=" match_parent " android:layout_height=" Match_ Parent " android:baselinealigned=" false " android:gravity=" center " android:orientation=" Horizontal " android:weightsum= "1" > <!--1. Set LinearLayout layout_weight to 1--and <!--2. The Layout_ of the button Width is set to 0DP, and Layout_weight is set to 0.5- <button android:layout_width= "0DP" android:layout_ height= "Wrap_content" android:layout_weight= "0.5" android:textsize= "13sp" android:text= "@string /button_text "> </Button></LinearLayout>
Here are a few key points:
- The layout is linearlayout so that layout_weight can be set.
- Set the value of the LinearLayout android:weightsum to 1;
- Set the button's layout_width to 0DP;
- Set the value of the layout_weight of the button to 0.5. Which is half the android:weightsum above.
you can implement the layout in the above settings.
The width of the subspace algorithm can be used for example the following formula, child control width = child control layout_width + child control Layout_weight * Parent control's width/parent control's layout_weight, compared to the above example is the child control width = 0.5 * The parent control's Width/1 = parent control width * 0.5. Because the width of the parent control is match_parent, the width of the parent control width is the width of the entire screen, so the width of the child control = 0.5 * Width of the entire screen = half screen width.
Set a half screen size and center button layout in Android (Layout_weight property)