Android布局设置两个垂直和水平居中的按钮

Van*_*nel 0 android android-layout

我正在开发一个Android应用程序.

我正在设置按钮保存和关闭居中,但它们出现在左侧.

这是我的XML布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialogGameName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginBottom="2sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp"
        android:typeface="sans"
        android:textSize="26sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/dialogGameDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="2sp"
        android:typeface="monospace"
        android:textSize="18sp"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical|center_horizontal">
        <Button
            android:id="@+id/registerGameButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="5sp"
            android:layout_marginRight="2sp"
            android:layout_marginTop="0sp" />
        <Button
            android:id="@+id/closeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close" 
            android:gravity="center"
            android:layout_marginBottom="5sp"
            android:layout_marginLeft="2sp"
            android:layout_marginRight="5sp"
            android:layout_marginTop="0sp"/>
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 5

你不能真正集中任何东西,除非它的父母比它大.您将按钮的父级设置为wrap_content,因此无法居中...尝试使用按钮:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center">
    <Button
        android:id="@+id/registerGameButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="2sp"
        android:layout_marginTop="0sp" />
    <Button
        android:id="@+id/closeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="close" 
        android:layout_marginBottom="5sp"
        android:layout_marginLeft="2sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="0sp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)