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


猜你喜欢
- 将某个项目从Spring Boot1升级Spring Boot2之后出现如下报错,查了很多不同的解决方法都没有解决:Spring boot2
- 在窗体中添加DataGridView控件和ConTextMenuStrip1控件,修改DataGridView属性,将contextMenu
- 本文实例讲述了C#保存listbox中数据到文本文件的方法。分享给大家供大家参考。具体实现方法如下:private void SaveLst
- 通过eclipse修改web的url访问路径今天做SpringMVC 基础跳转网页的时候发现了一个问题,就是eclipse访问url路径的问
- 本文实例为大家分享了unity实现场景切换进度条显示的具体代码,供大家参考,具体内容如下一、UI。建立slider适当更改即可;二、新增lo
- 使用@Value取值出现的问题在springBoot项目中我们一般会把一些路径或者资源写在配置文件中,方便管理。但是取得时候有可能会出现一些
- 无论是我们在使用word还是记事本,系统都会为我们提供撤销的功能,这几乎是人人都会使用到的功能,而在我们实际开发中,会不会存在一个很复杂的对
- 效果展示单选版可看上篇博文 用flutter封装一个点击菜单工具栏组件本文是CHeckbox多选版效果如图所示,点击选项回调选中的
- springboot:接收date类型的参数今天有个postmapping方法,地址都正确,就是死活进不去,真是奇怪了。终于从日志中得出些端
- Bezier曲线的形状是通过一组多边折线(特征多边形)的各顶点唯一地定义出来的。在这组顶点中:(1)只有第一个顶点和最后一个顶点在曲线上;(
- 本文实例讲述了Android实现的数字格式化用法。分享给大家供大家参考,具体如下:package formatnumber;import j
- VAR 是3.5新出的一个定义变量的类型其实也就是弱化类型的定义VAR可代替任何类型编译器会根据上下文来判断你到底是想用什么类型的至于什么情
- 本文将介绍一段实例代码,来讲解利用正则表达式使C#判断输入日期格式是否正确的方法。希望这段代码能对大家有所帮助。 通常我们在用C#
- 主界面xml文件<RelativeLayout xmlns:android="http://schemas.android.
- 对象是对类的实例化。对象具有状态和行为,变量用来表明对象的状态,方法表明对象所具有的行为。Java 对象的生命周期包括创建、使用和清除。一、
- 一、静态代理模式1.1、 代理模式的定义:由于某些原因需要给某对象提供一个代理以控制对该对象的访问。这时,访问对象不适合或者不能直接引用目标
- 上代码喽~package ncu.com.app.chatpter_5;import java.util.Random;//结点类class
- MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。MongoD
- 引言Spring Boot的一个便捷功能是外部化配置,可以轻松访问属性文件中定义的属性。本文将详细介绍@ConfigurationPrope
- 一、序言Java多线程编程线程池被广泛使用,甚至成为了标配。线程池本质是池化技术的应用,和连接池类似,创建连接与关闭连接属于耗时操作,创建线