Android开发求助:Floating Action Button被底部导航栏遮挡
解决FAB显示在ListView上方且位于底部导航栏之上的问题
问题根源
- 底部导航栏的外层布局设置了
android:layout_height="match_parent",导致include后该布局会占满整个父容器,直接覆盖上方的FAB和ListView。 - 主布局中FrameLayout同时嵌套ListView、FAB和底部导航栏,布局层级混乱,底部导航因顺序靠后显示在最上层。
修复步骤
1. 修改底部导航栏布局(activity_base.xml)
将外层LinearLayout的高度改为wrap_content,避免其占满整个屏幕:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:context=".BaseActivity"> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="60dp" app:itemBackground="@color/orange" app:itemIconTint="@drawable/selector" app:itemTextColor="@drawable/selector" app:menu="@menu/menu_navigation" /> </LinearLayout>
2. 修改主页面布局(SessionsList的布局)
采用垂直LinearLayout拆分内容区域和底部导航栏,让内容区域占满剩余空间,FAB放在内容区域的FrameLayout中:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".SessionsList"> <!-- 内容区域:ListView + FAB --> <FrameLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <ListView android:id="@+id/lvSessions" android:layout_width="match_parent" android:layout_height="match_parent"/> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:src="@drawable/ic_baseline_add_24" android:contentDescription="" android:layout_margin="16dp" android:layout_marginBottom="76dp"/> <!-- 60dp导航栏高度 + 16dp间距 --> </FrameLayout> <!-- 底部导航栏 --> <include layout="@layout/activity_base"/> </LinearLayout>
可选方案:使用ConstraintLayout实现
如果偏好更灵活的布局方式,可改用ConstraintLayout直接通过约束定位元素:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SessionsList"> <ListView android:id="@+id/lvSessions" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/bottom_nav"/> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_add_24" android:contentDescription="" android:layout_margin="16dp" app:layout_constraintBottom_toTopOf="@id/bottom_nav" app:layout_constraintEnd_toEndOf="parent"/> <include android:id="@+id/bottom_nav" layout="@layout/activity_base" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
效果说明
修改后:
- ListView会占满底部导航栏以上的所有空间
- FAB固定在屏幕右下角,位于ListView上方且不会被底部导航栏遮挡
- 底部导航栏始终显示在屏幕最底部
内容的提问来源于stack exchange,提问作者Raz Dar
相关产品推荐
相关产品推荐

