在两个按钮之间添加空格

问题描述 投票:0回答:1

我有一个 LinearLayout,里面有两个按钮。按钮在布局中间相互接触,我想在它们之间添加一些空间。它们是水平方向的,我希望左侧按钮位于布局的最左侧,右侧按钮位于最右侧。

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="20dp">


            <Button
                android:id="@+id/truthDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/truthdialogbutton"
                android:text="truth" />

            <Button
                android:id="@+id/dareDialogButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/daredialogbutton"
                android:text="truth"/>
        </LinearLayout>

我尝试过不同的方法,例如增加布局和按钮的边距或填充,但似乎没有任何效果

android button android-linearlayout padding margin
1个回答
0
投票

在两个按钮中添加

android:layout_weight="1"
,并在这些按钮之间添加
Space

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">


    <Button
        android:id="@+id/truthDialogButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/truthdialogbutton"
        android:text="Truth" />

    <Space
        android:layout_width="20dp"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/dareDialogButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/daredialogbutton"
        android:text="Dare"/>
</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.