Android中使用bottomnavigation实现底部导航栏

1.Activity页面

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="#EFEDED"
    android:fitsSystemWindows="false"
    >

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation_1" />



        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@color/white"            导航栏背景颜色
            app:itemIconTint="@color/selector_color"     icon图片选中和未选中颜色
            app:itemTextColor="@color/selector_color"    文字选中和未选中颜色
            app:menu="@menu/bottom_navigation_menu"    
            app:labelVisibilityMode="labeled"
            android:layout_alignParentBottom="true"/>


</RelativeLayout>

2.创建menu页面 (将需要的图片放入mipmap中或drawable中)

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home"
        android:title="首页"
        android:checked="true"/>
    <item
        android:id="@+id/navigation_quanzi"
        android:icon="@drawable/jiedan_icon"
        android:title="接单" />
    <item
        android:id="@+id/navigation_dingdan"
        android:icon="@drawable/dingdan"
        android:title="订单" />
    <item
        android:id="@+id/navigation_mine"
        android:icon="@drawable/mine"
        android:title="我的" />

</menu>

3.在Activity的java文件中填写 点击事件(切换fragment页面)

java 复制代码
  private BottomNavigationView bottomNavigationView;   
  bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        ReplaceFragment(new HomeFragment());
                        return true;
                    case R.id.navigation_quanzi:
                        ReplaceFragment(new QuanZiFragment());
                        return true;
                    case R.id.navigation_dingdan:
                        ReplaceFragment(new DingDanFragment());
                        return true;
                    case R.id.navigation_mine:
                        ReplaceFragment(new MineFragment());
                        return true;
                }
                return false;
            }
        });

创建调用切换页面的方法

java 复制代码
  private void ReplaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction= fragmentManager.beginTransaction();
        transaction.replace(R.id.frame,fragment);
        transaction.commit();
    }
相关推荐
游戏开发爱好者82 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
王码码20352 小时前
Flutter for OpenHarmony 实战之基础组件:第三十一篇 Chip 系列组件 — 灵活的标签化交互
android·flutter·交互·harmonyos
黑码哥3 小时前
ViewHolder设计模式深度剖析:iOS开发者掌握Android列表性能优化的实战指南
android·ios·性能优化·跨平台开发·viewholder
亓才孓3 小时前
[JDBC]元数据
android
独行soc3 小时前
2026年渗透测试面试题总结-17(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
金融RPA机器人丨实在智能3 小时前
Android Studio开发App项目进入AI深水区:实在智能Agent引领无代码交互革命
android·人工智能·ai·android studio
科技块儿3 小时前
利用IP查询在智慧城市交通信号系统中的应用探索
android·tcp/ip·智慧城市
独行soc4 小时前
2026年渗透测试面试题总结-18(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
王码码20354 小时前
Flutter for OpenHarmony 实战之基础组件:第二十七篇 BottomSheet — 动态底部弹窗与底部栏菜单
android·flutter·harmonyos
2501_915106324 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview