layout_gravity 底部不起作用

layout_gravity bottom not working

我正在使用以下 XML 绘制带有标题、副标题、图像和按钮的布局。图像占用所有剩余的 space 但最后我将其设置为 visibility = View.GONE 并且我的按钮未与底部对齐。我做错了什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:baselineAligned="false">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/kasel"
        android:id="@+id/txtChokeNext"
        android:textSize="20sp"
        android:gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:layout_gravity="top"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/kaselDesc"
        android:id="@+id/txtChokeNextDesc"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:textStyle="bold"
        android:layout_gravity="top"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/imgChokeDesc"
        android:src="@drawable/kasel"
        android:adjustViewBounds="true"
        android:baselineAlignBottom="false"
        android:cropToPadding="false"
        android:layout_weight="1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/firstAidNext"
        android:id="@+id/buttonChokeNext"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="20dp"
        android:paddingRight="10dp"
        android:paddingBottom="20dp"
        android:layout_gravity="bottom"
        />

</LinearLayout>

感谢转发

您的问题的解决方案

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/kasel"
            android:id="@+id/txtChokeNext"
            android:textSize="20sp"
            android:gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_gravity="top" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/kaselDesc"
            android:id="@+id/txtChokeNextDesc"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:textStyle="bold"
            android:layout_gravity="top" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:id="@+id/imgChokeDesc"
            android:src="@drawable/kasel"
            android:adjustViewBounds="true"
            android:baselineAlignBottom="false"
            android:cropToPadding="false"
            android:layout_weight="1" />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/firstAidNext"
        android:id="@+id/buttonChokeNext"
        android:layout_marginTop="10dp"
        android:paddingLeft="10dp"
        android:paddingTop="20dp"
        android:paddingRight="10dp"
        android:paddingBottom="20dp"
        android:layout_gravity="bottom" />

</LinearLayout>

您可以将布局更改为 RelativeLayout 并将此参数设置为您的按钮:

android:layout_alignParentBottom="true"

您还应该在子标题和图像中添加 android:layout_below="..." 以将它们一个接一个地放置(将 ... 替换为应该在您正在编辑的元素上方的元素的 ID)