簡體   English   中英

如何在Android布局中使用邊距使按鈕居中?

[英]How to center a button with margin in android layout?

在android布局中,我想要兩行:

  1. 在第一行中,一個按鈕位於屏幕中間頂部的中央。 萊特(Ledt)和右邊的空間。

  2. 一排其他按鈕在該按鈕下方有一定距離。

以下是我嘗試使用的代碼。 對於第一個LinearLayout我嘗試了android:layout_gravity="center_vertical"以及android:layout_gravity="center_horizontal" ,然后在LinearLayout 第一個Button放入了一行android:layout_marginBottom="10dp" 我已將此答案用作模板。

但是,第一行和第二行中的按鈕之間沒有垂直空間,並且第一行(頂部)中的第一個按鈕在屏幕的左側 我以為我確實在xml文件中放入了正確的屬性,但是可能要復雜得多。 一如既往...

這是完整xml的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="10dp"
        >

        <Button
            android:id="@+id/list_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/ok"
            android:onClick="buttonEndStat"
            >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/header_list"
        android:layout_marginTop="36dp"
        >


        <Button
            android:id="@+id/listname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.225"
            android:layout_below="@id/list_ok"
            android:text="@string/headerName">
        </Button>
        <Button
            android:id="@+id/listok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerOK">
        </Button>
        <Button
            android:id="@+id/listnok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerNOK">
        </Button>
        <Button
            android:id="@+id/listhist"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/headerHist">
        </Button>
        <Button
            android:id="@+id/listprandom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.15"
            android:text="@string/headerP">
        </Button>
        <Button
            android:id="@+id/listphist"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerH">
        </Button>
        <Button
            android:id="@+id/listptot"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerProb">
        </Button>
        <Button
            android:id="@+id/listproc"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="%">
        </Button>

    </LinearLayout>

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_below="@id/header_list"
        >
    </ListView>

</RelativeLayout>

我將刪除第一個LinearLayout,僅將按鈕本身在屏幕上水平居中放置並留有一定的余量(我假設根元素是LinearLayout):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

        <Button
            android:id="@+id/list_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/ok"
            android:layout_gravity="center_horizontal"
            >
        </Button>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/header_list"
        android:layout_marginTop="36dp">

        <Button
            android:id="@+id/listname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.225"
            android:layout_below="@id/list_ok"
            android:text="@string/headerName">
        </Button>
        <Button
            android:id="@+id/listok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerOK">
        </Button>
        <Button
            android:id="@+id/listnok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerNOK">
        </Button>
        <Button
            android:id="@+id/listhist"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/headerHist">
        </Button>
        <Button
            android:id="@+id/listprandom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.15"
            android:text="@string/headerP">
        </Button>
        <Button
            android:id="@+id/listphist"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerH">
        </Button>
        <Button
            android:id="@+id/listptot"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerProb">
        </Button>
        <Button
            android:id="@+id/listproc"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="%">
        </Button>

    </LinearLayout>

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_below="@id/header_list"
        >
    </ListView>

</LinearLayout>

如果要保留第一個LinearLayout, layout_gravity從其中刪除屬性layout_gravity ,然后將android:layout_gravity="center_horizontal"到按鈕中,以使其水平居中。

編輯:

如果使用RelativeLayout作為根元素,則必須指定每個子項的位置。 您忘記將“ header_list”放置在第一行以下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/top_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center_horizontal"
        >

        <Button
            android:id="@+id/list_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:text="@string/ok"
            android:onClick="buttonEndStat"
            >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/header_list"
        android:layout_below="@id/top_row"
        >


        <Button
            android:id="@+id/listname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.225"
            android:layout_below="@id/list_ok"
            android:text="@string/headerName">
        </Button>
        <Button
            android:id="@+id/listok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerOK">
        </Button>
        <Button
            android:id="@+id/listnok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:text="@string/headerNOK">
        </Button>
        <Button
            android:id="@+id/listhist"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="@string/headerHist">
        </Button>
        <Button
            android:id="@+id/listprandom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.15"
            android:text="@string/headerP">
        </Button>
        <Button
            android:id="@+id/listphist"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerH">
        </Button>
        <Button
            android:id="@+id/listptot"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="@string/headerProb">
        </Button>
        <Button
            android:id="@+id/listproc"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="0.075"
            android:text="%">
        </Button>

    </LinearLayout>

    <ListView
        android:id="@+id/listview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_below="@id/header_list"
        >
    </ListView>

</RelativeLayout>

要將按鈕水平居中,請將android:gravity="center_horizontal"到其父項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM