AndroidX项目中BottomNavigationView显示为空且高度为0问题求助
问题分析与解决方案
你遇到的问题核心出在FrameLayout的布局属性错误上,咱们一步步拆解:
- 你的FrameLayout写了
android:layout_above="@id/frameLayout",这是一个自引用的低级错误——你让FrameLayout布局在它自己的上方,这在布局计算时会导致逻辑混乱,最终FrameLayout会占据整个RelativeLayout的高度(match_parent),直接把底部的BottomNavigationView完全遮挡,或者让系统无法正确计算BottomNavigationView的高度,导致它显示为空、高度为0。
修正步骤
1. 给BottomNavigationView添加唯一ID
首先给你的BottomNavigationView设置一个专属ID,比如android:id="@+id/bottom_nav",这样FrameLayout可以正确引用它的位置。
2. 修正FrameLayout的layout_above属性
把FrameLayout的android:layout_above="@id/frameLayout"改成android:layout_above="@id/bottom_nav",这样FrameLayout会自动布局在BottomNavigationView的上方,不会再遮挡底部导航栏。
修正后的Activity布局XML
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/bottom_nav"/> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation"/> </RelativeLayout>
额外检查项(可选)
- 确认
com.google.android.material:material:1.1.0依赖已经正确同步到项目中,没有同步失败的提示; - 检查你的Activity是否继承了
AppCompatActivity(AndroidX环境下必须),Material组件依赖AppCompat主题才能正常工作; - 确认你的App主题是Material系列主题(比如
Theme.MaterialComponents.DayNight.NoActionBar),否则BottomNavigationView可能无法正常渲染样式。
这样修改后,BottomNavigationView应该就能正常显示出你配置的菜单项了~
内容的提问来源于stack exchange,提问作者ZeroByter
相关产品推荐
相关产品推荐

