Android 使用BottomNavigationView和Novigation组件实现底部导航功能

本文主要是介绍Android 使用BottomNavigationView和Novigation组件实现底部导航功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
👉点击跳转到网站

效果图如下:

在这里插入图片描述

实现步骤:

在工作之前添加所需的相关依赖,切记这个一定不要忘了

implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'

一、首先做准备工作,创建4个Fragment和对应的布局文件,和menu文件
1.四个Fragment我拿出一个供大家参考

FirstFragment类

public class FirstFragment extends Fragment {private FirstViewModel mViewModel;public static FirstFragment newInstance() {return new FirstFragment();}@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.first_fragment, container, false);}@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);mViewModel = new ViewModelProvider(this).get(FirstViewModel.class);// TODO: Use the ViewModel}}

对应的first_fragment.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".fragment.FirstFragment"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="笔记"android:textSize="30sp" /></LinearLayout>

2、接下来就是在menu文件夹下创建my_bottom_navigation.xml:
这里面就是放置底部导航的图标和对应的文字。
特别说明:这里的图标颜色可以随意,没有限制,因为后面可以在BottomNavigationView标签中设置具体的图标选中前和选中后的颜色。
这里我用的是矢量图,优点就是没有任何锯齿,在任何尺寸的屏幕上都可以更好的展示图标。

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:id="@+id/firstFragment"android:icon="@drawable/first_normal"android:title="笔记" /><itemandroid:id="@+id/secondFragment"android:icon="@drawable/second_normal"android:title="信息" /><itemandroid:id="@+id/thirdFragment"android:icon="@drawable/third_normal"android:title="电话" /><itemandroid:id="@+id/fourthFragment"android:icon="@drawable/fourth_normal"android:title="我的" />
</menu>

二、接下来在res目录下,创建资源类型为Navigation,名称为可以自定义,这里设置为了my_bottom_navigation.xml

<?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/my_bottom_navigation"app:startDestination="@id/firstFragment"><fragmentandroid:id="@+id/firstFragment"android:name="com.example.exampletest.fragment.FirstFragment"android:label="first_fragment"tools:layout="@layout/first_fragment" /><fragmentandroid:id="@+id/secondFragment"android:name="com.example.exampletest.fragment.SecondFragment"android:label="second_fragment"tools:layout="@layout/second_fragment" /><fragmentandroid:id="@+id/thirdFragment"android:name="com.example.exampletest.fragment.ThirdFragment"android:label="third_fragment"tools:layout="@layout/third_fragment" /><fragmentandroid:id="@+id/fourthFragment"android:name="com.example.exampletest.fragment.FourthFragment"android:label="fourth_fragment"tools:layout="@layout/fourth_fragment" />
</navigation>

在这里面说明一下:

  1. app:startDestination=“@id/firstFragment” 属性:默认进来之后的一个页面,就是FirstFragment这个页面。
  2. label 这个属性表示标签 就是标题栏中的字体,我这里因为设置了主题为没有ActionBar,所以自然就没有这个效果
  3. 这里的每个fragment标签的id一定要跟menu文件夹下my_bottom_navigation.xml文件中item标签中的id一一对应

三、创建BottomNavigationViewActivity和对应的activity_bottom_navigation_view.xml,把这个四个Fragment柔和在一起,并实现点击对应的图标切换到对应的界面的功能。

1.首先activity_bottom_navigation_view.xml的实现 非常重要的一步

<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".BottomNavigationViewActivity"><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/bottomNavigationView"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/white"app:itemIconTint="@color/text_color"app:itemTextColor="@color/text_color"app:labelVisibilityMode="labeled"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:menu="@menu/my_bottom_navigation" /><fragmentandroid:id="@+id/fragment8"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="0dp"app:defaultNavHost="true"app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/my_bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

分析里面比较重要的属性:
1.fragment标签下的:

  1. app:layout_constraintBottom_toTopOf=“@+id/bottomNavigationView” 作用:fragment的下边和底部导航的上边对齐,换句话说就是fragment在其底部导航的上边
  2. app:defaultNavHost=“true” 表示使用默认的导航host,自动覆盖Activity的back按钮
  3. app:navGraph="@navigation/my_bottom_navigation"指向你的navigation文件夹下的my_bottom_navigation文件

2.BottomNavigationView标签下的

  1. app:itemIconTint=“@color/text_color” 表示选中和非选中状态下图标的颜色
  2. app:itemTextColor=“@color/text_color” 表示选中和非选中状态下文字的颜色 所以这两个属性用同一个资源文件就行了
  3. app:labelVisibilityMode=“labeled” 当底部导航图标为四个或者五个时,最多也只能设置五个,就要设置这个属性,显示图标标记,说白了就是显示图标下面的文字,如果不设置这个属性的话,非选中状态下,图标下面的文字是无法显示的

2、BottomNavigationViewActivity这个文件下的代码就很简单了

public class BottomNavigationViewActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_bottom_navigation_view);//实例化BottomNavigationView BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);//获取NavController实例 //NavController:负责操作Navigation框架下的Fragment的跳转与退出、动画、监听当前Fragment信息//这个R.id.fragment8就是Activity布局里fragment控件的idNavController controller = Navigation.findNavController(this, R.id.fragment8);//在选择菜单时调用,当目的地更改时,BottomNavigationView中的选定项将自动更新 NavigationUI.setupWithNavController(bottomNavigationView, controller);}
}

这样就使用BottomNavigationView完成了一个基本的底部导航功能,在这个模板上面,就可以加自己需要的功能了,当然,有不当之处可以在评论区指正,不敢误人子弟~

这篇关于Android 使用BottomNavigationView和Novigation组件实现底部导航功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/321435

相关文章

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python虚拟环境终极(含PyCharm的使用教程)

《Python虚拟环境终极(含PyCharm的使用教程)》:本文主要介绍Python虚拟环境终极(含PyCharm的使用教程),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录一、为什么需要虚拟环境?二、虚拟环境创建方式对比三、命令行创建虚拟环境(venv)3.1 基础命令3

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi

关于pandas的read_csv方法使用解读

《关于pandas的read_csv方法使用解读》:本文主要介绍关于pandas的read_csv方法使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录pandas的read_csv方法解读read_csv中的参数基本参数通用解析参数空值处理相关参数时间处理相关

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

SpringBoot条件注解核心作用与使用场景详解

《SpringBoot条件注解核心作用与使用场景详解》SpringBoot的条件注解为开发者提供了强大的动态配置能力,理解其原理和适用场景是构建灵活、可扩展应用的关键,本文将系统梳理所有常用的条件注... 目录引言一、条件注解的核心机制二、SpringBoot内置条件注解详解1、@ConditionalOn

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(