android - 如果在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。
推荐阅读
- java - Spring Boot 2.0.3 Oauth2 安全性:即使在标头中使用访问令牌时也会出现 401 错误
- sql - 如何仅将数据从一个数据库表复制到sql server中的另一个数据库现有表
- mysql - “SELECT * FROM System”的 MySQL 语法错误 1064
- templates - 在 terraform 中进行配置之前操作文件
- npm - 获取最近安装的包列表 - Yarn / NPM
- https - CouchDB 无法设置 HTTPS
- r - r - 将数据帧滞后 NA 的数量
- google-cloud-platform - 无法增加每个项目配额的数据流作业
- c# - 如何在 ContextMenu 中获取 PlacementTarget.Content?
- powershell - 将生成的价值传递给下游作业