这是我的布局代码;

<?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它正是如此,不留空间的其他项目。如何获取这是首次在一个线性布局,从布局的其余项目所需的最小间隔填满整个空间的项目?


相对布局确实答案:

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

可以通过线性布局内嵌套相对布局保持你的初始线性布局:

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

上面的回答(通过雅努什)是相当正确的,但是我personnally不觉得100%舒适与RelativeLayouts,所以我更喜欢以引入“填料”,空TextView的,如下所示:

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

这应该是在屏幕的底部的元件之前。

您可以用的LinearLayout或滚动型做到这一点,太。有时,它是很容易,一个RelativeLayout的实现。你需要做的唯一事情是前您希望查看,添加以下视图的对齐到屏幕的底部:

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

此创建一个空的视图,填充空的空间,推动下一个视图的屏幕的底部。

此也适用。

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

重力= “底部” 浮动的LinearLayout元件底部

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)中设置的子LinearLayoutandroid: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>

输出

“在此输入图像的描述”

在跟进 Timores优雅的溶液时,我已经发现,下面创建在垂直的LinearLayout的垂直填充和在水平的LinearLayout一个横向填充:

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

您甚至不需要嵌套的第一个内部的第二relative布局。简单地使用android:layout_alignParentBottom="true"按钮的EditText

如果你不希望做很多变化,那么你可以只是把:

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>

创建两个页脚,这里是一个例子:

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

截图

“在此输入图像的描述”

下面的代码使用。对齐按钮BUTTOM。它的工作。

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

有关这样的情况下,总是使用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>

使用android:layout_alignParentBottom="true"<RelativeLayout>

这有一定帮助。

在的情况下,你有一个这样的层次结构:

<ScrollView> 
  |-- <RelativeLayout> 
    |-- <LinearLayout>

首先,运用android:fillViewport="true"ScrollView,然后应用android:layout_alignParentBottom="true"LinearLayout

这工作对我来说非常完美。

<ScrollView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scrollbars="none"
    android:fillViewport="true">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/linearLayoutHorizontal"
            android:layout_alignParentBottom="true">
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

您可以只给你顶子视图(TextView的 @ + ID / TextView的)的属性:  android:layout_weight="1"

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

我使用的溶液雅努什贴,但我添加填充到最后一个视图,因为我的布局的顶部是一个滚动型

在滚动型将被部分地隐藏,因为它与内容的增加。在最后一个视图使用android:paddingBottom有助于显示了滚动的所有内容。

此可以用线性布局做得太。

只需提供高度= 0dp和重量= 1到上述的布局和你在底部要的目的地。刚写入高度=涡卷内容和没有重量。

它提供包裹内容的布局(包含您的编辑文本和按钮的一个),然后在出现重量的一个占据的布局的其余部分。

我偶然发现了这一点。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top