这是我的布局代码;

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

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

Android布局-实际(左)和期望(右)

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

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

如果线性布局中的第一个项目被告知填充父项,那么它会完全填充父项,不会为其他项目留下空间.如何获得线性布局中的第一个项目,以填充布局中其他项目所需的最小空间之外的所有空间?


相对布局确实是答案:

    <?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的底部(使用101

下面的示例创建了一个浮动操作按钮,该按钮将与屏幕的末端和底部对齐.

<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之前,答案是relative layout分.


如果您有一个占满整个屏幕的相对布局,您应该能够使用android:layout_alignParentBottom将按钮移动到屏幕底部.

如果底部的视图没有显示在相对布局中,那么上面的布局可能占据了所有空间.在这种情况下,您可以将视图(应该位于底部)放在布局文件中的第一个位置,并将布局的其余部分放置在视图上方android:layout_above的位置.这使得底部视图能够占据所需的空间,布局的其余部分可以填充屏幕的所有其余部分.

Android相关问答推荐

当我的Log.i()不在主线程中时,如何在我的logcat中显示它?

写入排除例外以进行依赖性判断

AdMob:MobileAds. initialize()—java. lang. xml对于某些设备不能强制转换为java. lang. String""

替换- prop -中的值(adb shell getprop)

在卡片上创建圆角底部边框

避免在按下肯定按钮时自动取消AlertDialog

StateFlow集合AsState没有更新他的值Jetpack Compose导航

Android:微调:在代码中设置ArrayAdapter不希望调用On ItemSelected,仅当用户单击微调时调用

两首合成曲的喷气背包动画

Jetpack Compose主导航中的全屏图标列表

在内部创建匿名对象的繁忙循环调用函数会产生开销吗?

Android 12+BLE字节不同

如何使用其他组件的位置来定位自定义弹出窗口

仅当先前输入为 yes 时,Android 才会要求下一个输入

任务:app:kaptGenerateStubsDebugKotlin执行失败. > 'compileDebugJavaWithJavac' 任务(当前目标是 1.8)

如何在 Jetpack Compose 中向图像视图添加对角色带?

为什么项目捕获对象给我在 Compose 中找不到参考

在移动设备上看到时如何增加 PasswordField 文本?

如何在 Jetpack Compose 中添加多个标签

Android - 水平(从右到左)圆形背景 colored颜色 过渡