这是我的布局代码;
<?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会在页面底部重叠ScrollView中的任何内容。
我用一个动态拉伸的FrameLayout修复了它:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<!-- content goes here -->
<!-- stretching frame layout, using layout_weight -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<!-- content fixated to the bottom of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- your bottom content -->
</LinearLayout>
</LinearLayout>
</ScrollView>
1. 在根布局中使用ConstraintLayout
并设置app:layout_constraintBottom_toBottomOf="parent",让布局在屏幕底部:
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
</LinearLayout>
2. 在根布局中使用FrameLayout
只需在布局中设置android:layout_gravity="bottom"
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
3.使用LinearLayout在你的根布局(android:orientation="vertical")
(1)在你的布局顶部设置一个布局android:layout_weight="1"
<TextView
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="welcome" />
(2)为android设置子LinearLayout:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"
主属性是ndroid:gravity="bottom",让子视图位于布局的底部。
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
</LinearLayout>
4. 根布局中使用RelativeLayout
并设置android:layout_alignParentBottom="true",让布局在屏幕底部
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
</LinearLayout>
输出