Android 如何在屏幕底部对齐视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2386866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:38:57  来源:igfitidea点击:

How do I align views at the bottom of the screen?

androidxmluser-interfaceandroid-layout

提问by gav

Here's my layout code;

这是我的布局代码;

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

What this looks like is on the left and what I want it to look like is on the right.

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

Android Layout - Actual (Left) and Desired (Right)

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

The obvious answer is to set the TextView to fill_parent on height, but this causes no room to be left for the button or entry field.

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

Essentially the issue is that I want the submit button and the text entry to be a fixed height at the bottom and the text view to fill the rest of the space. Similarly, in the horizontal linear layout I want the submit button to wrap its content and for the text entry to fill the rest of the space.

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

If the first item in a linear layout is told to fill_parent it does exactly that, leaving no room for other items. How do I get an item which is first in a linear layout to fill all space apart from the minimum required by the rest of the items in the layout?

如果线性布局中的第一个项目被告知 fill_parent,它就是这样做的,不会为其他项目留下空间。如何获得一个在线性布局中首先填充除布局中其余项目所需的最小空间之外的所有空间的项目?



Relative layouts were indeed the answer:

相对布局确实是答案:

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

采纳答案by Janusz

The modern way to do this is to have a ConstraintLayoutand constrain the bottom of the view to the bottom of the ConstraintLayout with app:layout_constraintBottom_toBottomOf="parent"

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

The example below creates a FloatingActionButton that will be aligned to the end and the bottom of the screen.

下面的示例创建了一个 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>

For reference, I will keep my old answer.

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

Before the introduction of ConstraintLayout the answer was a relative layout.

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



If you have a relative layout that fills the whole screen you should be able to use android:layout_alignParentBottomto move the button to the bottom of the screen.

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

If your views at the bottom are not shown in a relative layout then maybe the layout above it takes all the space. In this case you can put the view, that should be at the bottom, first in your layout file and position the rest of the layout above the views with android:layout_above. This enables the bottom view to take as much space as it needs, and the rest of the layout can fill all the rest of the screen.

如果底部的视图没有以相对布局显示,那么它上面的布局可能会占用所有空间。在这种情况下,您可以将视图放在底部,首先放在布局文件中,然后将布局的其余部分放置在视图上方android:layout_above。这使得底部视图可以根据需要占用尽可能多的空间,并且布局的其余部分可以填充屏幕的所有其余部分。

回答by Bram Avontuur

In a ScrollViewthis doesn't work, as the RelativeLayoutwould then overlap whatever is in the ScrollViewat the bottom of the page.

在 aScrollView这不起作用,因为它RelativeLayoutScrollView与页面底部的任何内容重叠。

I fixed it using a dynamically stretching FrameLayout:

我使用动态拉伸修复了它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>

回答by pseudosudo

You can keep your initial linear layout by nesting the relative layout within the linear layout:

您可以通过在线性布局中嵌套相对布局来保持初始线性布局:

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

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button android:text="submit" 
            android:id="@+id/Button" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">
        </Button>
        <EditText android:id="@+id/EditText" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/Button"
            android:layout_alignParentBottom="true">
        </EditText>
    </RelativeLayout>
</LinearLayout>

回答by Timores

The answer above (by Janusz) is quite correct, but I personnally don't feel 100% confortable with RelativeLayouts, so I prefer to introduce a 'filler', empty TextView, like this:

上面的答案(由 Janusz 提供)是非常正确的,但我个人对 RelativeLayouts 并不感到 100% 舒适,所以我更喜欢引入一个“填充器”、空 TextView,如下所示:

<!-- filler -->
<TextView android:layout_height="0dip" 
          android:layout_width="fill_parent"
          android:layout_weight="1" />

before the element that should be at the bottom of the screen.

在应该位于屏幕底部的元素之前。

回答by keybee

You can do this with a LinearLayout or a ScrollView, too. Sometimes it is easier to implement than a RelativeLayout. The only thing you need to do is to add the following view beforethe Views you want to align to the bottom of the screen:

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

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

This creates an empty view, filling the empty space and pushing the next views to the bottom of the screen.

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

回答by Michael Reed

This also works.

这也有效。

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

gravity="bottom" to float LinearLayout elements to bottom

weight="bottom" 将 LinearLayout 元素浮动到底部

回答by KeLiuyue

1. Use ConstraintLayoutin your root Layout

1.ConstraintLayout在你的根布局中使用

And set app:layout_constraintBottom_toBottomOf="parent"to let the Layout on the bottom of the screen:

并设置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. Use FrameLayoutin your root Layout

2.FrameLayout在你的根布局中使用

Just set android:layout_gravity="bottom"in your layout

只需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. Use LinearLayoutin your root Layout (android:orientation="vertical")

3.LinearLayout在你的根布局中使用 ( android:orientation="vertical")

(1) Set a layout android:layout_weight="1"on the top of the your Layout

(1)android:layout_weight="1"在你的 Layout 顶部设置一个布局

<TextView
    android:id="@+id/TextView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:text="welcome" />

(2) Set the child LinearLayoutfor android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"

(2)设置孩子LinearLayoutandroid:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"

The main attribute is ndroid:gravity="bottom", let the child View on the bottom of Layout.

主要属性是ndroid:gravity="bottom",让子View在Layout的底部。

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

4. Use RelativeLayoutin the root Layout

4.RelativeLayout在根布局中使用

And set android:layout_alignParentBottom="true"to let the Layout on the bottom of the screen

并设置android:layout_alignParentBottom="true"让 Layout 在屏幕底部

<LinearLayout
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">
</LinearLayout>

Output

输出

Enter image description here

在此处输入图片说明

回答by stevehs17

Following up on Timores's elegant solution, I have found that the following creates a vertical fill in a vertical LinearLayout and a horizontal fill in a horizontal LinearLayout:

跟进Timores 的优雅解决方案,我发现以下内容在垂直 LinearLayout 中创建了垂直填充,在水平 LinearLayout 中创建了水平填充:

<Space
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />

回答by Steve Haley

You don't even need to nest the second relativelayout inside the first one. Simply use the android:layout_alignParentBottom="true"in the Buttonand EditText.

您甚至不需要relative在第一个布局中嵌套第二个布局。只需android:layout_alignParentBottom="true"ButtonEditText 中使用

回答by Kiran Parmar

If you don't wish to make many changes, then you could just put:

如果您不想进行太多更改,则可以输入:

android:layout_weight="1"

for the TextView having ID as @+id/TextViewi.e

对于 ID 为@+id/TextViewie的 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>