如果在 Android 中占据整个屏幕,如何滚动和查看列表视图下方的元素?

How to scroll and view an element below a listview if it takes up the entire screen in Android?

我的布局中有一个结帐购物车 ListView,如果用户决定添加大量商品,它可能会占据整个屏幕。但是,如果 listView 已满,则无法访问 ListView 下方的结帐按钮。这是一个示例布局:

<RelativeLayout 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/cartListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"/>

    <Button
        android:id="@+id/checkOutButton"
        android:onClick="checkOutButtonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:background="@drawable/custom_button_service"
        android:text="@string/service_findMe_button"
        android:layout_gravity="center"
        android:textColor="@android:color/white"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_below="@id/cartListView"
        android:textStyle="normal" />
</RelativeLayout>

您正在寻找 NestedScrollView。您的布局最终应如下所示:

<RelativeLayout 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

            <ListView
                android:id="@+id/cartListView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="16dp"/>

            <Button
                android:id="@+id/checkOutButton"
                android:onClick="checkOutButtonClick"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="100dp"
                android:layout_marginRight="100dp"
                android:background="@drawable/custom_button_service"
                android:text="@string/service_findMe_button"
                android:layout_gravity="center"
                android:textColor="@android:color/white"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:layout_below="@id/cartListView"
                android:textStyle="normal" />

        </LinearLayout>

    </NestedScrollView>

</RelativeLayout>

一些补充建议: ListView 很老派,所以我建议您使用 RecyclerView