这是我的布局代码;

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

    <TextView android:text="@string/welcome"
        android:id="@+id/TextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </TextView>

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

            <EditText android:id="@+id/EditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </EditText>

            <Button android:text="@string/label_submit_button"
                android:id="@+id/Button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </Button>

    </LinearLayout>

</LinearLayout>

左边是这个样子,右边是我想要的样子。

显而易见的答案是将TextView设置为高度上的fill_parent,但这导致没有空间留给按钮或输入字段。

本质上的问题是,我希望提交按钮和文本条目在底部有一个固定的高度,而文本视图填充其余的空间。类似地,在水平线性布局中,我希望提交按钮包装其内容,并让文本条目填充其余空间。

如果线性布局中的第一项被告知fill_parent,它就会执行fill_parent,不为其他项留下任何空间。我如何得到一个项目,这是第一个在线性布局,以填补所有空间除了最小要求的其余项目在布局?


相对布局确实是答案:

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

    <TextView
        android:text="@string/welcome"
        android:id="@+id/TextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">
    </TextView>

    <RelativeLayout
        android:id="@+id/InnerRelativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:text="@string/label_submit_button"
            android:id="@+id/Button"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </Button>

        <EditText
            android:id="@+id/EditText"
            android:layout_width="fill_parent"
            android:layout_toLeftOf="@id/Button"
            android:layout_height="wrap_content">
        </EditText>

    </RelativeLayout>

</RelativeLayout>

当前回答

现代的方法是使用一个ConstraintLayout将视图的底部约束到ConstraintLayout的底部使用app:layout_constraintBottom_toBottomOf="parent"

下面的例子创建了一个FloatingActionButton,它将对齐到屏幕的末端和底部。

<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_height="match_parent"
   android:layout_width="match_parent">

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"

    app:layout_constraintBottom_toBottomOf="parent"

    app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

作为参考,我将保留我的旧答案。

在引入ConstraintLayout之前,答案是相对布局。


如果你有一个填充整个屏幕的相对布局,你应该能够使用android:layout_alignParentBottom将按钮移动到屏幕底部。

如果你在底部的视图没有显示在一个相对的布局,那么上面的布局可能会占用所有的空间。在这种情况下,你可以把视图,那应该在底部,首先在你的布局文件和位置上的其余布局与android:layout_above视图。这使得底部视图可以占用尽可能多的空间,而布局的其余部分可以填充屏幕的其余部分。

其他回答

如果你有一个这样的层次结构:

<ScrollView> 
  |-- <RelativeLayout> 
    |-- <LinearLayout>

首先,应用android:fillViewport="true"到ScrollView,然后应用android:layout_alignParentBottom="true"到LinearLayout。

这对我来说非常有效。

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scrollbars="none"
    android:fillViewport="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/linearLayoutHorizontal"
            android:layout_alignParentBottom="true">
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

你也可以用LinearLayout或ScrollView来做这个。有时候它比RelativeLayout更容易实现。你唯一需要做的就是在你想要对齐到屏幕底部的视图之前添加以下视图:

<View
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />

这将创建一个空视图,填充空白空间并将下一个视图推到屏幕底部。

我使用了Janusz发布的解决方案,但我在最后一个视图中添加了填充,因为我的布局的顶部是一个ScrollView。

ScrollView会随着内容的增长而部分隐藏。在最后一个视图上使用android:paddingBottom有助于显示ScrollView中的所有内容。

这也是可行的。

<LinearLayout 
    android:id="@+id/linearLayout4"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/linearLayout3"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" 
    android:gravity="bottom"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="20dp"
>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 

    />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" 


    />

</LinearLayout>

线性布局也可以做到这一点。

只需要为上面的布局和底部的布局提供Height = 0dp和weight = 1即可。只写高度=包装内容,不写重量。

它为布局提供换行内容(包含编辑文本和按钮的内容),然后有权重的内容占据布局的其余部分。

这是我偶然发现的。