当前位置: 首页 > news >正文

Android 底部导航栏 (BottomNavigationView) 制作教程

在 Android 应用开发中,底部导航栏是非常常见的交互组件,能够让用户快速切换不同的功能模块。本文将详细介绍如何使用 Material Design 组件中的 BottomNavigationView 实现底部导航功能。

目录结构如图

一、布局文件实现

首先,我们需要在 activity_main.xml 中定义主布局结构,主要包含一个用于显示内容的 FrameLayout 和底部导航栏 BottomNavigationView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 用于显示Fragment内容的容器 --><FrameLayoutandroid:id="@+id/fragment_container"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><!-- 底部导航栏 --><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/bottom_nav"android:layout_width="match_parent"android:layout_height="wrap_content"app:menu="@menu/bottom_nav_menu" />
</LinearLayout>

二、创建导航菜单

接下来需要创建底部导航栏的菜单文件,在 res/menu 目录下创建 bottom_nav_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/nav_home"android:icon="@drawable/ic_home"android:title="Home" /><itemandroid:id="@+id/nav_dashboard"android:icon="@drawable/ic_dashboard"android:title="Dashboard" /><itemandroid:id="@+id/nav_notify"android:icon="@drawable/ic_notify"android:title="Notify" />
</menu>

三、添加导航图标

在 res/drawable 目录下添加三个导航图标文件(可以使用 Vector Asset 创建):

  1. ic_home.xml(首页图标)
  2. <vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><pathandroid:fillColor="#FF000000"android:pathData="M10,20v-6h4v6h5v-8h3L12,3 2,12h3v8z" />
    </vector>
  3. ic_dashboard.xml(仪表盘图标)
  4. <vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><pathandroid:fillColor="#FF000000"android:pathData="M3,13h8L11,3L3,3v10zM3,21h8v-6L3,15v6zM13,21h8L21,11h-8v10zM13,3v6h8L21,3h-8z" />
    </vector>
  5. ic_notify.xml(通知图标)
  6. <vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24.0"android:viewportHeight="24.0"><pathandroid:fillColor="#FF000000"android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z" />
    </vector>

    四、创建 Fragment 布局

  7. 我们需要为三个导航项创建对应的 Fragment 布局文件:

  8. fragment_home.xml(首页布局):
  9. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="首页"android:textSize="24sp" />
    </LinearLayout>
  10. fragment_dashboard.xml(仪表盘布局):
  11. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="仪表盘"android:textSize="24sp" />
    </LinearLayout>
  12. fragment_notify.xml(通知布局):
  13. <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通知"android:textSize="24sp" />
    </LinearLayout>

    五、创建 Fragment 类

    为每个布局创建对应的 Fragment 类:

  14. HomeFragment.java:
  15. import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import androidx.fragment.app.Fragment;public class HomeFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// 加载布局文件return inflater.inflate(R.layout.fragment_home, container, false);}
    }

    其他代码也一样 注意修改 类名和 布局选择

六、实现 MainActivity 逻辑

最后,在 MainActivity 中实现底部导航栏的切换逻辑:

package com.example.demo01;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {private BottomNavigationView bottomNav;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bottomNav = findViewById(R.id.bottom_nav);// 默认首页loadFragment(new HomeFragment());// 1.2.1 旧版监听接口bottomNav.setOnNavigationItemSelectedListener(item -> {Fragment selected = null;int id = item.getItemId();if (id == R.id.nav_home) {selected = new HomeFragment();} else if (id == R.id.nav_dashboard) {selected = new DashboardFragment();} else if (id == R.id.nav_notify) {selected = new NotifyFragment();}return loadFragment(selected);});}private boolean loadFragment(Fragment f) {if (f == null) return false;getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();return true;}
}

七、注意事项

  1. 底部导航栏的图标和文字颜色可以通过主题进行自定义

  2. 当导航项超过 3 个时,BottomNavigationView 会默认使用移位模式,可通过代码禁用

http://www.dtcms.com/a/592893.html

相关文章:

  • 当“老龄化”撞上“数字化”:金融业的“续命解药”还是“增长新图”?银行保险AI人工智能数字化营销销售培训老师培训讲师
  • ArrayList与LinkedList的比较
  • 魏公村网站建设建设部网站电话
  • MySQL索引篇 -- 从数据页的角度看B+树
  • 使用 Newtonsoft.Json(Json.NET)库将对象导出为格式化的 JSON 文件
  • 林州市网站建设给个网站好人有好报
  • 使用前端框架vue做一个小游戏
  • 【操作系统原理】进程优先级与命令行参数、环境变量详解
  • 【深度学习新浪潮】扩散模型中,VAE潜空间正则化如何为生成带来帮助?
  • 从零学习Node.js框架Koa 【四】Koa 与数据库(MySQL)连接,实现CRUD操作
  • Zotero在代理模式下无法同步问题
  • LeetCode(python)——438.找到字符串中所有字母异位词
  • 解决添加asp.net网站报错请添加 MIME 映射
  • 浙江省工程建设管理协会网站常州小程序开发公司
  • ASP vs ASP.NET vs ASP.NET Core:三代微软 Web 技术核心区别解析
  • 【项目设计】基于正倒排索引的Boost搜索引擎
  • 建网站需要几程序员关键词网站优化平台
  • 深圳网站建设方案书做sns网站需要什么
  • C语言常见推理题
  • leetcode 3542. 将所有元素变为 0 的最少操作次数 中等
  • 一文掌握,sward安装与配置
  • Supabase 开源 BaaS 平台的技术内核与实践指南
  • YOLOv5+DeepSORT目标检测
  • 通过Prometheus对GPU集群进行监控以及搭建(小型集群)
  • 【datawhale】Agentic AI学习笔记
  • 江苏国龙翔建设公司网站找工作网站建设
  • 网站建设及在线界面设计
  • Aloha浏览器 7.10.1 |私人浏览器,极速上网,资源嗅探
  • 多Agent协同-详解
  • Spring Boot 数据库操作实战:MyBatis 让 CRUD 像 “查奶茶库存” 一样简单