如何将按钮放在滚动视图的底部

How to put button on the bottom of Scrollview

我试过搜索所有内容,但我似乎无法弄清楚这一点。基本上,我有一个带有滚动视图和其中元素的布局。我想在 end/bottom 处的滚动视图中放置一个按钮,只有当您一直滚动到底部时才能看到它。 我试过这样做:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/topcontainer">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar_review"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:title="Review Your Massage">

        </android.support.v7.widget.Toolbar>


        <include layout="@layout/card_review"/>

    </LinearLayout>


    <Button
        android:id="@+id/buttonReview"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent"
        android:text="@string/book"
        android:layout_below="@+id/topcontainer"
        />
</RelativeLayout>
</ScrollView>

按钮确实在底部,但它下面有 space。如果我 运行 在 phone 分辨率更高的 phone 上使用该应用程序,按钮将几乎位于屏幕中间。我怎样才能让它无论分辨率如何都始终保持在底部?

这是目前的截图:

对于 android:layout_height="match_parent"android:fillViewport="true"ScrollView 使用所有可用的 space,即使 children 更小。

Space 有一个 android:layout_weight="1" 来接收额外的 space,所以按钮总是在底部。

包含更多详细信息的 explanation by Romain Guy

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:fillViewport="true"
            android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/topcontainer">

            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/toolbar_review"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:title="Review Your Massage">

            </android.support.v7.widget.Toolbar>

            <include layout="@layout/card_review"/>

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="0dp" />

            <Button
                android:id="@+id/buttonReview"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@color/colorAccent"
                android:text="@string/book" />
        </LinearLayout>

</ScrollView>

试试这个

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true" 
    android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/topcontainer">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_review"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:title="Review Your Massage">

    </android.support.v7.widget.Toolbar>


    <include layout="@layout/card_review"/>

</LinearLayout>


<Button
    android:id="@+id/buttonReview"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:background="@color/colorAccent"
    android:text="@string/book"
    />

</RelativeLayout>
</ScrollView>