Android 布局-两个按钮上的一个按钮

Android 布局-两个按钮上的一个按钮,android,android-layout,Android,Android Layout,我对安卓系统缺乏经验,而且我的布局应该是底部有3个按钮,以这种方式显示: 我几乎做到了这样,只是根据屏幕的不同,底部的按钮要么重叠,要么太远。按钮应该在“应用”按钮下方完全对齐,但我似乎无法正确对齐 以下是我当前的代码: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="20dp">

我对安卓系统缺乏经验,而且我的布局应该是底部有3个按钮,以这种方式显示:

我几乎做到了这样,只是根据屏幕的不同,底部的按钮要么重叠,要么太远。按钮应该在“应用”按钮下方完全对齐,但我似乎无法正确对齐

以下是我当前的代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="20dp">

    <RelativeLayout
        android:id="@+id/relapply"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/apply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/apply_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:width="320dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="bottom"
        android:layout_below="@id/relapply">

        <Button
            android:id="@+id/discard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/discard_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_marginStart="30dp"
            android:width="154dp"
            android:layout_alignParentStart="true"/>

        <RelativeLayout
            android:layout_width="12dp"
            android:layout_height="30dp"
            android:layout_toLeftOf="@id/save">
        </RelativeLayout>

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/save_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_marginEnd="30dp"
            android:width="154dp"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>

</RelativeLayout>

这是我的按钮所在布局的代码。然后所有内容都在我制作的线性布局xml测试文件中。关于如何改进这一点有什么建议吗


谢谢

首先,你有办法获得比你需要的更多的相对布局。 您可以在这里尝试这种布局(为了演示,我删除了背景并对文本进行了硬编码)


这确保了两个按钮的水平跨度相等(因为它们具有相同的宽度)。如果您希望整个布局具有特定的宽度,只需将相对布局中的android:layout\u width=“match\u parent”设置为android:layout\u width=“320dp”

首先,您可以获得比您需要的更多的相对布局。 您可以在这里尝试这种布局(为了演示,我删除了背景并对文本进行了硬编码)

这确保了两个按钮的水平跨度相等(因为它们具有相同的宽度)。如果您希望整个布局具有特定的宽度,只需将相对布局中的android:layout\u width=“match\u parent”设置为android:layout\u width=“320dp”

,您可以执行以下操作:

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/background_floating_material_dark">

    <Button
        android:id="@+id/apply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple_buttons_carousel"
        android:text="@string/apply_button"
        android:textSize="15sp"
        android:textColor="@android:color/white"
        android:layout_marginBottom="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/discard"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/discard_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_weight="0.9" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"></LinearLayout>

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/save_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_weight="0.9" />

    </LinearLayout>
</LinearLayout>

输出接近于此:

您可以执行以下操作:

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:background="@color/background_floating_material_dark">

    <Button
        android:id="@+id/apply"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ripple_buttons_carousel"
        android:text="@string/apply_button"
        android:textSize="15sp"
        android:textColor="@android:color/white"
        android:layout_marginBottom="10dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2">

        <Button
            android:id="@+id/discard"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/discard_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_weight="0.9" />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"></LinearLayout>

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@drawable/ripple_buttons_carousel"
            android:text="@string/save_button"
            android:textSize="15sp"
            android:textColor="@android:color/white"
            android:layout_weight="0.9" />

    </LinearLayout>
</LinearLayout>

输出接近于此:


您也可以使用以下代码。。。。。 这很简单

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <Button
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="Apply" />

        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="5dp"
             android:orientation="horizontal"
             android:weightSum="2">

             <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="5dp"
                  android:layout_weight="1"
                  android:text="Discard" />

             <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="5dp"
                  android:layout_weight="1"
                  android:text="Save" />

        </LinearLayout>

     </LinearLayout>

您也可以使用以下代码。。。。。 这很简单

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp">

        <Button
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="Apply" />

        <LinearLayout
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_marginTop="5dp"
             android:orientation="horizontal"
             android:weightSum="2">

             <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="5dp"
                  android:layout_weight="1"
                  android:text="Discard" />

             <Button
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="5dp"
                  android:layout_weight="1"
                  android:text="Save" />

        </LinearLayout>

     </LinearLayout>


成功了,谢谢!我知道在这里练习举重是有意义的,但我没有机会尝试。这就成功了,谢谢!我知道在这里练习举重是有意义的,但我没有机会尝试。