BottomNavigation底部导航

创建三个Fragment类 HomeFragment、DashboardFragment、NotificationsFragment 用于底部按钮切换时的页面

  • 创建 res --> menu下 New Menu Resources File 新建bottom_nav_menu.xml



    

    

    


  • 新建导航文件 res --> navigation包(Navigation Resource File) --> mobile_navigation.xml



    

    

    

  1. 将底部菜单bottom_nav_menu.xml 与 页面关联在一起
  • 再xml中:



    
    

    
    


其中底部导航容器 引用底部菜单 bottom_nav_menu.xml
中间页面切换容器 引用页面导航 mobile_navigation.xml

  • Activity
//底部菜单
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
//配置底部三个菜单导航
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
                .build();

//中间页面切换
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

// 将上面菜单与页面整合起来
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);

其中 AppBarConfiguration 简化写法

//配置底部三个菜单导航
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                bottomNavigationView.getMenu())
                .build();

你可能感兴趣的:(BottomNavigation底部导航)