这是我的布局代码;

<?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>

当前回答

对于这种情况,总是使用RelativeLayouts。线性布局不是为这样的用法而设计的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/db1_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <!-- Place your layout here -->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >

        <Button
            android:id="@+id/setup_macroSavebtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Save" />

        <Button
            android:id="@+id/setup_macroCancelbtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />

        </LinearLayout>

</RelativeLayout>

其他回答

如果你不想做太多改动,你可以输入:

android:layout_weight="1"

对于有ID的TextView @+ ID /TextView,即

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

现代的方法是使用一个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视图。这使得底部视图可以占用尽可能多的空间,而布局的其余部分可以填充屏幕的其余部分。

您甚至不需要将第二个相对布局嵌套在第一个相对布局中。简单地使用android:layout_alignParentBottom="true"在按钮和EditText。

你可以给你的顶级子视图(TextView @+id/TextView)一个属性: android: layout_weight =“1”。

这将迫使它下面的所有其他元素移到底部。

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

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