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();
    }
相关推荐
启雀AI35 分钟前
培训平台移动端离线学习方案设计与实现:视频缓存、断点续传与进度同步的工程实践
android·学习·缓存·音视频·企业lms
2601_966563523 小时前
愤怒的小鸟单机版 去广告 安卓+PC端离线纯净版 益智耐玩小游戏 不需联网 老旧手机都可以玩 怀旧手机小游戏
android·智能手机
hunterandroid4 小时前
[Android 从零到一] Retrofit 请求取消与生命周期绑定:从 Call.cancel 到协程可取消设计
android
爱跑马的程序员4 小时前
安卓专有的通信子系统-Binder IPC
android·binder·ipc·安卓间通信机制
ue星空4 小时前
【安卓逆向】为什么用Frida?
android
小驰行动派5 小时前
Camx架构全景图:从V4L2到Pipeline的完整拆解
android·camera·android camera
hunterandroid6 小时前
[Android 从零到一] Custom View 触摸反馈与手势冲突解决
android
coderSong25687 小时前
Android | 四大组件之 BroadcastReceiver(广播接收器)
android
没伞的行者8 小时前
《C++ Primer Plus(第六版)全书代码整理 | 章节源码 + README知识总结(Gitee、GitHub开源)》
linux·开发语言·c++·gitee·github
7177779 小时前
让权限治理成为可复制资产:Gitee Team 空间配置方案与安全级别落地详解
数据库·gitee