Android mobile phone development uses linear layout and relative layout to realize Button vertical and horizontal centering
Center, there are two different layout of the center! They are LinearLayout and RelativeLayout.
1. The first thing to say is the center under LinearLayout layout:
Note: In the android: layout_width= “fill_parent” android: layout_height= “fill_parent” attribute, if the level is centered, it will occupy at least the full screen in width; If vertically centered, it occupies full screen in height
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|center_horizontal|center_vertical" >
// Above gravity Parameters of the property: center For the center, center_horizontal Is horizontally centered, center_vertical Is vertically centered
<Button
android:id="@+id/Binding_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Associate a new account " />
</LinearLayout>
2. Then we talk about the center under the RelativeLayout layout:
<RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
Android:layout_width="fill_parent" Android:layout_height="fill_parent">
<Button Android:id="@+id/btngal" Android:layout_width="wrap_content"
Android:layout_height="wrap_content" Android:gravity="center_horizontal"
Android:textSize="20sp" Android:layout_alignParentBottom="true"
Android:layout_centerHorizontal="true" Android:text=" Return to the main interface " />
</RelativeLayout>
Brief description
Android: gravity= “CENTER_VERTICAL”: This is vertical center alignment
Android: gravity= “BOTTOM”: At the bottom of the container
Android: gravity= “CENTER”: In the center of the container
3. 1 Method with two buttons in the center of the row
Method 1:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/ButtonBar">
<Button android:id="@+id/btn_listview"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="ListView"/>
<Button android:id="@+id/btn_emptyview"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="EmptyView"/>
</LinearLayout>
Method 2:
<RelativeLayout android:id="@+id/relativeTop"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/btnGetMp3s"
android:text="@string/strGetMp3List"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_toLeftOf="@id/txtHide" />
<TextView android:id="@+id/txtHide" android:layout_width="25" android:layout_height="1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnExitSys"
android:text="@string/strExitSys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtHide" />
</RelativeLayout>