android - 加载项目时如何设置recyclerview的背景?
问题描述
我的应用程序中有一个 recyclerview,我想将其设置background
为黑色。我已经尝试使用该background
属性设置 recyclerview 的背景,但它只在具有项目的 recyclerview 部分设置黑色背景。
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:listitem="@layout/item_card">
</androidx.recyclerview.widget.RecyclerView>
如果回收站视图没有填满整个屏幕,则底部始终为白色。当活动刚刚打开并且 recyclerview 的项目仍在加载时,recyclerview 仅显示为空白(白色)空间。
你基本上如何设置recyclerview的背景?
编辑:这是整个布局
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/spacing_middle"
android:paddingEnd="@dimen/spacing_middle"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:listitem="@layout/item_card">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:layout_gravity="center"
android:indeterminateTint="@color/white"
android:progressDrawable="@drawable/circular_progress_bar" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
解决方案
将 recyclerview 放在滚动视图中从来都不是一个好主意,也不需要。您遇到问题的原因是因为您尝试在没有定义高度的东西上匹配父级,因为它在滚动视图中。
实际上,您需要的唯一两件事是 RV 和进度条,使用相对布局并将 RV 对齐到顶部,将 PB 对齐到底部并将 RV 设置为 PB 上方的布局。将相对布局的颜色设置为黑色,您根本不需要为 RV 着色。
推荐阅读
- javascript - 无法访问状态对象(React useState hook)
- sql - 如何在同一查询中使用多个不同的计数与 Druid SQL 中的其他列?
- ios - 如何创建基于对象的渐变着色器,而不是纹理或面?
- python - 通过python检查网站是否有效
- next.js - 在搜索中按 Enter 时数据不会更新
- docker - rabbitmq-server - 与非 root 用户分离被阻塞
- python - 如何创建检查点并从检查点或保存的 GAN 模型中恢复权重?
- angular - 为我的项目创建类似 jql 的解析器的问题
- c++ - 制作地板图案
- python - pandas:多级列数据框 - 如何更改特定级别的名称?