let's look at the following layout:
, the button is half the size of the screen, then centered in the center of the layout, everyone has their own answers, see my method, Layout layout XML is as follows:
<?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 the layout_weight of LinearLayout to 1--and <!--2. Set the Button's layout_width to 0DP and layout_weight to 0.5 and <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 the 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 button's layout_weight to 0.5, which is half of the android:weightsum above.
you can implement the layout in the above settings. The width of the subspace algorithm can refer to 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 width of the child control = 0.5 * The width of the parent control /1 = parent Control width * 0.5, because the width of the parent control is match_parent, so 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.