如何在Scrollview底部放置按钮

问题描述 投票:0回答:3

我已经尝试到处搜索,但我似乎无法弄清楚这一点。基本上,我有一个带有 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="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>

按钮确实在底部,但下面有空间。如果我在分辨率更高的手机上运行该应用程序,该按钮将几乎位于屏幕中间。我怎样才能让它无论分辨率如何它总是留在底部?

这是目前的屏幕截图:

android xml android-layout android-scrollview
3个回答
26
投票

使用

android:layout_height="match_parent"
android:fillViewport="true"
,即使孩子较小,
ScrollView
也会使用所有可用空间。

Space
有一个
android:layout_weight="1"
来容纳额外的空间,因此按钮始终位于底部。

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>

1
投票

试试这个

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

0
投票

在滚动视图中使用下划线,高度应为 match_parent。

 android:fillViewport="true"
© www.soinside.com 2019 - 2024. All rights reserved.