android实现简单底部导航栏
作者:穷少年 发布时间:2022-07-10 16:11:08
标签:android,导航栏
本文实例为大家分享了android实现底部导航栏的具体代码,供大家参考,具体内容如下
常见的底部导航栏
动态效果
实现步骤
1.底部导航栏样式
我们应该在项目的res文件夹下新建一个menu文件夹,用来装menu布局文件
<?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/international_1"
android:title="主页" />
<item
android:id="@+id/navigation_edit"
android:icon="@drawable/edit_0"
android:title="发布" />
<item
android:id="@+id/navigation_view"
android:icon="@drawable/view_0"
android:title="关注" />
<item
android:id="@+id/navigation_user"
android:icon="@drawable/user_0"
android:title="我的" />
</menu>
2.新建四个fragment组件
每一个fragment的组件内容相同
四个fragement对应的layout
四个fragment布局文件的内容也相同,写上内容以区别是哪个页面
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="this is homebar"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
3.建议navigation布局文件(至关重要)
这个文件指定了页面上显式那些fragment组件
在项目res下新建一个文件夹专门用来存放此文件
id取值一定要与底部导航栏样式里面指定的ID相同,因为android自动根据底部按钮的ID来绑定按钮与fragment
<?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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="cn.liuhao.test.fragments.HomeFragment"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_view"
android:name="cn.liuhao.test.fragments.ViewFragment"
tools:layout="@layout/fragment_view" />
<fragment
android:id="@+id/navigation_edit"
android:name="cn.liuhao.test.fragments.EditFragment"
tools:layout="@layout/fragment_eidt" />
<fragment
android:id="@+id/navigation_user"
android:name="cn.liuhao.test.fragments.UserFragment"
tools:layout="@layout/fragment_user" />
</navigation>
4.activity
布局文件
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<!-- 底部导航栏 -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<!-- 页面中显式fragment的容器-->
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
内容(绑定Navigation与BottomNavigationView)
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// 获取页面上的底部导航栏控件
BottomNavigationView navView = findViewById(R.id.nav_view);
// 配置navigation与底部菜单之间的联系
// 底部菜单的样式里面的item里面的ID与navigation布局里面指定的ID必须相同,否则会出现绑定失败的情况
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home,R.id.navigation_edit,R.id.navigation_view,R.id.navigation_user)
.build();
// 建立fragment容器的控制器,这个容器就是页面的上的fragment容器
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// 启动
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
来源:https://blog.csdn.net/qq_42418169/article/details/117305196
0
投稿
猜你喜欢
- openFeign服务间调用保持请求头信息处理1、注意特殊情况,在定时任务或者内部之间调用,没有request的时候,不要处理直接返回。2、
- 1. Easy Rules 概述Easy Rules是一个Java规则引擎,灵感来自一篇名为《Should I use a Rules En
- 1.两种取值方式的差异mapper.xml映射文件<select id="selectEmployeeByCondition
- 简介本文主要介绍如何使用java代码利用Selenium操作浏览器,某些网页元素加载慢,如何操作元素就会把找不到元素的异常,此时需要设置元素
- java 高并发中volatile的实现原理摘要: 在多线程并发编程中synchronized和Volatile都扮演着重要的角色,Vola
- java匿名内部类:1:匿名内部类,匿名内部类也就是没有名字的内部类。2:匿名内部类的作用正因为没有名字,所以匿名内部类只能使用一次,它通常
- 如今代码圈很多做网络爬虫的例子,今天小编给大家分享的是如何用C#做网络爬虫。注意这次的分享只是分享思路,并不是一整个例子,因为如果要讲解一整
- 获取自定义菜单查询返回的结果有乱码解决方法:string Posturl = "https://api.weixin.qq.com
- 配置Thymeleaf的模板路径众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templa
- java 反射机制:测试实体类以Human为例/** * Project: Day12_for_lxy * Created: Lulu *
- 本文实例介绍了基于socket和javaFX简单文件传输工具,分享给大家供大家参考,具体内容如下package application;im
- 使用ModelAndView向request域对象共享数据index.html<a th:href="@{/testMode
- malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的
- 背景大家在使用Selenium + Chromedriver爬取网站信息的时候,以为这样就能做到不被网站的反爬虫机制发现。但是实际上很多参数
- 本文实例讲述了Android实现内存中数据保存到sdcard的方法。分享给大家供大家参考,具体如下:public static void w
- List集合相信大家在开发过程中几乎都会用到。有时候难免会遇到集合里的数据是重复的,需要进行去除。然而,去重方式有好几种方式,你用的是哪种方
- 选取单个元素直觉来说选取单个元素肯定会比选取多个要简单得多,不过这里也存在一些问题。我们先看下一般的做法的问题是什么,然后再看下如何用lam
- 这一篇网络爬虫的实现就要联系上大数据了。在前两篇java实现网络爬虫和heritrix实现网络爬虫的基础上,这一次是要完整的做一次数据的收集
- FormClosing事件在窗体关闭时,FormClosing事件发生。此事件会得到处理。从而释放与窗体相关的所有资源。如果取消此事件,则窗
- 1. 配置MailPlugin插件public void configPlugin(PluginLoader pluginLoader) {