无法将按钮定位到屏幕底部

Unable to position a button to the bottom of screen

我试图将底部的两个按钮("Reset" 和 "Apply")对齐,但它始终将自己与屏幕顶部对齐

`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/form_bg">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:id="@+id/strp"
        android:orientation="horizontal">

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

            <com.widget.AppButton
                android:id="@+id/country_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Country"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/domain_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dip"
                android:text="Domain"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/unit_filter"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Unit"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />


        </LinearLayout>

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/white_divider" />

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/black_divider" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/strp"
        android:layout_alignParentBottom="true">

        <com.widget.AppButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset"
            android:textStyle="bold"
            android:id="@+id/button"
            style="@style/btn"
            android:layout_weight="1"/>

        <com.widget.AppButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Apply"
            android:textStyle="bold"
            android:id="@+id/button2"
            style="@style/btn"
            android:layout_weight="1"/>
    </LinearLayout>


</RelativeLayout>`

我当然已经将 layout_alignParentBottom 设置为 true,但问题仍然存在

建议您将 Relativelayout 更改为 Linearlayout,因为您不需要使用它提供额外的属性并从该布局文件中删除无效参数:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/form_bg"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/strp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

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

            <com.widget.AppButton
                android:id="@+id/country_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Country"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/domain_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dip"
                android:text="Domain"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/unit_filter"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Unit"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />


        </LinearLayout>

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/white_divider" />

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/black_divider" />

    </LinearLayout>

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

        <com.widget.AppButton
            android:id="@+id/button"
            style="@style/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Reset"
            android:textStyle="bold" />

        <com.widget.AppButton
            android:id="@+id/button2"
            style="@style/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Apply"
            android:textStyle="bold" />
    </LinearLayout>


</LinearLayout>

android:layout_alignBottom=" " 在相对布局中效果更好,尝试在相对布局中使用它。还要在按钮上而不是在布局上使用 android:layout_alignBottom=" "

<RelativeLayout // Changed it to RL
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true" // Removed AlignBottom
    android:orientation="horizontal">

    <com.widget.AppButton
        android:id="@+id/button"
        style="@style/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignBottom="true" // this one
        android:text="Reset"
        android:textStyle="bold" />

    <com.widget.AppButton
        android:id="@+id/button2"
        style="@style/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignBottom="true" // This one
        android:text="Apply"
        android:textStyle="bold" />
</RelativeLayout>

您应该将最后一个 LinearLayout 更改为

  • 移除android:layout_alignBottom="@+id/strp"
  • android:layout_height="match_parent" 更改为 android:layout_height="wrap_content"

所以代码应该是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/form_bg">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:id="@+id/strp"
        android:orientation="horizontal">

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

            <com.widget.AppButton
                android:id="@+id/country_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Country"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/domain_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dip"
                android:text="Domain"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />

            <com.widget.AppButton
                android:id="@+id/unit_filter"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/button_config"
                android:gravity="left"
                android:padding="10dp"
                android:text="Unit"
                android:textColor="#000"
                android:textSize="32sp"
                android:textStyle="italic" />


        </LinearLayout>

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/white_divider" />

        <View
            android:layout_width="1px"
            android:layout_height="match_parent"
            android:background="@drawable/black_divider" />

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <com.widget.AppButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reset"
            android:textStyle="bold"
            android:id="@+id/button"
            style="@style/btn"
            android:layout_weight="1"/>

        <com.widget.AppButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Apply"
            android:textStyle="bold"
            android:id="@+id/button2"
            style="@style/btn"
            android:layout_weight="1"/>
    </LinearLayout>


</RelativeLayout>