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

相关文章

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Java中实现订单超时自动取消功能(最新推荐)

《Java中实现订单超时自动取消功能(最新推荐)》本文介绍了Java中实现订单超时自动取消功能的几种方法,包括定时任务、JDK延迟队列、Redis过期监听、Redisson分布式延迟队列、Rocket... 目录1、定时任务2、JDK延迟队列 DelayQueue(1)定义实现Delayed接口的实体类 (

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2

Java使用Tesseract-OCR实战教程

《Java使用Tesseract-OCR实战教程》本文介绍了如何在Java中使用Tesseract-OCR进行文本提取,包括Tesseract-OCR的安装、中文训练库的配置、依赖库的引入以及具体的代... 目录Java使用Tesseract-OCRTesseract-OCR安装配置中文训练库引入依赖代码实

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数