Android中如何设置RecyclerView高度避免自定义标签栏遮挡列表项
问题原因
你的页面根布局使用了CoordinatorLayout,其中承载Fragment的FrameLayout(@+id/fragment_content)设置了android:layout_height="match_parent",会占满整个父容器高度,而底部的BottomAppBar是悬浮在FrameLayout上层的,因此Fragment中的RecyclerView底部区域自然会被BottomAppBar遮挡。
解决方法
提供两种可直接落地的方案,按需选择即可:
方案1:修改Activity全局布局(推荐,一次修改所有Fragment生效)
通过CoordinatorLayout的Insets避让属性,让FrameLayout自动避开底部导航区域,修改后的对应代码如下:
<FrameLayout android:id="@+id/fragment_content" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_dodgeInsetEdges="bottom" /> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" app:fabCradleMargin="20dp" app:fabCradleVerticalOffset="10dp" app:fabCradleRoundedCornerRadius="20dp" android:layout_gravity="bottom" app:layout_insetEdge="bottom" >
这个方案会让所有放入fragment_content的Fragment内容都自动避开底部导航栏,无需单独修改每个Fragment的布局,适配性更强。
方案2:单独修改Dashboard页面的RecyclerView配置
如果只需要当前页面避开底部栏,不需要全局生效,可以给RecyclerView添加底部内边距,同时关闭padding裁剪保证滚动效果正常:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="80dp" android:clipToPadding="false"/>
- 其中
paddingBottom的数值可以根据你的BottomAppBar实际高度调整,一般设置为80dp可以覆盖大部分底部导航的高度场景 clipToPadding="false"的作用是让RecyclerView滚动时内容可以显示在padding区域,不会出现底部留白的割裂感
内容的提问来源于stack exchange,提问作者Learn2Code
相关产品推荐
相关产品推荐

