BottomNavigationView显示为滚动菜单问题求助
嘿,我一眼就瞅出问题所在了:你在布局里用的是NavigationView(这是侧边导航栏控件),但你想要实现的是底部导航栏的BottomNavigationView,这就是为啥它会显示成滚动菜单的原因!
下面是具体的修改步骤和修正后的代码:
1. 替换主布局控件并调整属性
把原来的NavigationView换成BottomNavigationView,同时调整布局参数适配底部导航的需求:
<android.support.constraint.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=".PostOrSearch"> <android.support.design.widget.BottomNavigationView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:menu="@menu/manu_layout" /> <!-- 哦对了,要是你菜单文件名是手滑写成`manu`而非`menu`的话,记得改成和实际文件名一致! --> </android.support.constraint.ConstraintLayout>
关键修改点:
- 控件类型从侧边导航的
NavigationView改成底部导航专用的BottomNavigationView - 宽度设为
match_parent适配屏幕,高度用wrap_content自适应内容 - 移除了
android:layout_gravity="start"、app:layout_insetEdge="bottom"这些侧边导航才需要的属性,只保留底部对齐的必要约束
2. 确认菜单XML的正确性
你的菜单XML本身没问题,这里再贴一遍方便对照:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="Perfil" android:icon="@drawable/perfil"/> <item android:title="Search" android:icon="@drawable/search"/> <item android:title="Post" android:icon="@drawable/megaphone"/> </menu>
额外小提醒
要是你已经迁移到AndroidX了,记得把控件换成com.google.android.material.bottomnavigation.BottomNavigationView,同时确保build.gradle里引入了Material组件的依赖哦。
这样修改后,就能得到你想要的底部导航栏样式啦!
内容的提问来源于stack exchange,提问作者Marco
相关产品推荐
相关产品推荐

