You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Bottom Navigation无法切换页面,请问遗漏了什么步骤?

Bottom Navigation View无法切换页面的问题解决

使用Android Navigation组件实现Bottom Navigation View时,点击菜单项既不会选中也不会切换页面,操作步骤如下:

  1. 创建四个仅包含TextView的Fragment;
  2. 导航图代码:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/main_nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.baba_abdullah.Project.fragment.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/addBooksFragment"
        android:name="com.example.Project.fragment.AddBooksFragment"
        android:label="fragment_add_books"
        tools:layout="@layout/fragment_add_books" />

    <fragment
        android:id="@+id/chatFragment"
        android:name="com.example.Project.fragment.ChatFragment"
        android:label="fragment_chat"
        tools:layout="@layout/fragment_chat" />

    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.Project.fragment.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
</navigation>
  1. 菜单文件代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/home_fragment"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="@string/home_menu_title"/>
    <item
        android:id="@+id/add_book_fragment"
        android:icon="@drawable/ic_baseline_add_circle_outline_24"
        android:title="@string/add_book_menu_title"/>
    <item
        android:id="@+id/chat_fragment"
        android:icon="@drawable/ic_baseline_chat_bubble_outline_24"
        android:title="@string/chat_menu_title"/>
    <item
        android:id="@+id/profile_fragment"
        android:icon="@drawable/ic_baseline_account_circle_24"
        android:title="@string/profile_menu_title"/>
</menu>
  1. MainActivity布局代码:
<?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:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/main_fragment_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_nav_graph" />


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_bar"
        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/main_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. MainActivity代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.main_fragment_container);
    NavController navController = navHostFragment.getNavController();
    BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_bar);
    NavigationUI.setupWithNavController(bottomNav, navController);
}

核心问题

导航图中Fragment的ID与BottomNavigationView菜单Item的ID不匹配。NavigationUI.setupWithNavController的工作逻辑是通过ID完全匹配来关联菜单项与对应Fragment,两者ID不一致时无法建立映射关系。

对比两者ID:

  • 导航图Fragment ID:homeFragment、addBooksFragment、chatFragment、profileFragment
  • 菜单Item ID:home_fragment、add_book_fragment、chat_fragment、profile_fragment

解决办法

修改其中一方的ID,让两者一一对应即可。例如修改菜单文件的Item ID,使其与导航图Fragment ID一致:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/homeFragment"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="@string/home_menu_title"/>
    <item
        android:id="@+id/addBooksFragment"
        android:icon="@drawable/ic_baseline_add_circle_outline_24"
        android:title="@string/add_book_menu_title"/>
    <item
        android:id="@+id/chatFragment"
        android:icon="@drawable/ic_baseline_chat_bubble_outline_24"
        android:title="@string/chat_menu_title"/>
    <item
        android:id="@+id/profileFragment"
        android:icon="@drawable/ic_baseline_account_circle_24"
        android:title="@string/profile_menu_title"/>
</menu>

修改完成后重新运行应用,点击BottomNavigationView的菜单项即可正常切换页面并选中对应项。

内容的提问来源于stack exchange,提问作者ganjaam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:25:24