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

gav*_*gav 627 xml user-interface android android-layout

这是我的布局代码;

<?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>
Run Code Online (Sandbox Code Playgroud)

这看起来像在左边,我希望它看起来像在右边.

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

显而易见的答案是在高度上将TextView设置为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>
Run Code Online (Sandbox Code Playgroud)

Jan*_*usz 519

执行此操作的现代方法是使用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>
Run Code Online (Sandbox Code Playgroud)

作为参考,我会保留旧答案.
在引入ConstraintLayout之前,答案是相对布局.


如果您的相对布局填满整个屏幕,则应该可以使用android:layout_alignParentBottom此按钮将按钮移动到屏幕底部.

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

  • 这是正确的ScrollViews和相对布局不能很好地混合.如果你有很多内容要在一个页面上显示所有内容,只需使用线性布局并最后放在底部视图中.如果您希望视图在小屏幕上的滚动视图中显示在最后,而在较大手机上的页面底部,请考虑使用不同的布局文件并使用ressource限定符让设备选择正确的布局文件. (6认同)
  • 仅供参考:在ScrollView中无法使用.这是我现在面临的问题.请参阅http://stackoverflow.com/questions/3126347/android-relativelayout-how-to-alignparentbottom-when-wrapped-in-a-scrollview (5认同)

小智 150

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>
Run Code Online (Sandbox Code Playgroud)

  • 我在我的应用程序中使用你的解决方案,但在我的情况下,我希望当键盘出现时,按钮停留在屏幕的底部(键盘后面)有一个问题吗?Thaks. (4认同)
  • 如果您希望底部永久固定,那么这一直无法显示.否则它有效. (2认同)

pse*_*udo 70

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个混乱的方法,因为它不是很模块化.你不应该有重叠的布局.如果您想要更改RelativeLayout的背景或想要使任何视图更大或添加视图,此设计将无法运行. (2认同)

Tim*_*res 42

上面的答案(由Janusz提供)是非常正确的,但我个人并不觉得100%与RelativeLayouts相容,所以我更喜欢引入一个'填充',空TextView,如下所示:

<!-- filler -->
<TextView android:layout_height="0dip" 
          android:layout_width="fill_parent"
          android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

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


key*_*bee 31

您也可以使用LinearLayout或ScrollView执行此操作.有时,实现RelativeLayout更容易.您唯一需要做的就是想要对齐到屏幕底部的视图之前添加以下视图:

<View
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

这将创建一个空视图,填充空白区域并将下一个视图推送到屏幕底部.


Mic*_*eed 27

这也有效.

<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>
Run Code Online (Sandbox Code Playgroud)

gravity =

  • android:layout_alignParentBottom ="true"无效,除非LinearLayout嵌套在RelativeLayout中. (12认同)

KeL*_*yue 22

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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

主要属性是ndroid:gravity="bottom",让子视图在Layout的底部.

<LinearLayout
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="horizontal">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

OUTPUT

在此输入图像描述


ste*_*s17 18

继Timores的优雅解决方案之后,我发现以下内容在垂直LinearLayout中创建了一个垂直填充,并在水平LinearLayout中创建了一个水平填充:

<Space
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)


Ste*_*ley 15

您甚至不需要将第二个relative布局嵌套在第一个布局中.使用时只需android:layout_alignParentBottom="true"按钮EditText上.


Kir*_*mar 11

如果你不想做很多改变,那么你可以放:

android:layout_weight="1"
Run Code Online (Sandbox Code Playgroud)

对于具有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>
Run Code Online (Sandbox Code Playgroud)


Mad*_*ota 7

创建HEADER和FOOTER,这是一个例子

[布局XML]

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/backgroundcolor"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="#FF0000">
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:background="#FFFF00">
    </RelativeLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

[截图]

在此输入图像描述

希望这是有帮助的.谢谢!!


sha*_*nal 5

使用下面的代码。将按钮与按钮对齐。它正在工作。

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

    <Button
        android:id="@+id/btn_back"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:text="Back" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.97"
        android:gravity="center"
        android:text="Payment Page" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Submit"/>
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Shu*_* A. 5

对于这种情况,请始终使用 RelativeLayouts。LinearLayout 不适用于此类用途。

<?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>
Run Code Online (Sandbox Code Playgroud)