android实现简单底部导航栏

作者:穷少年 时间:2022-07-10 16:11:08 

本文实例为大家分享了android实现底部导航栏的具体代码,供大家参考,具体内容如下

常见的底部导航栏

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>

android实现简单底部导航栏

2.新建四个fragment组件

每一个fragment的组件内容相同

android实现简单底部导航栏

四个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>

android实现简单底部导航栏

3.建议navigation布局文件(至关重要)

这个文件指定了页面上显式那些fragment组件

在项目res下新建一个文件夹专门用来存放此文件

android实现简单底部导航栏

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

标签:android,导航栏
0
投稿

猜你喜欢

  • 简单说说Java SE、Java EE、Java ME三者之间的区别

    2022-01-20 06:05:10
  • c# Newtonsoft 六个值得使用的特性(下)

    2022-02-18 11:38:32
  • Java实现几种序列化方式总结

    2023-02-13 06:18:27
  • Spring Native打包本地镜像的操作方法(无需通过Graal的maven插件buildtools)

    2023-11-25 04:24:30
  • Unity3D实现分页系统

    2022-06-28 22:53:37
  • 配置Ant执行Jmeter脚本过程详解

    2023-11-09 22:35:29
  • spring springMVC中常用注解解析

    2023-09-14 20:45:46
  • Android实现拍照或者选取本地图片

    2022-06-16 03:58:31
  • Java中二维数组的正确使用方法介绍

    2023-11-19 16:14:18
  • 详解如何用c++实现平衡二叉树

    2023-11-30 21:29:44
  • SpringBoot框架中Mybatis-plus的简单使用操作汇总

    2022-12-17 19:10:53
  • 鉴权认证+aop+注解+过滤feign请求的实例

    2022-06-05 14:25:34
  • 解读@RabbitListener起作用的原理

    2021-08-04 12:58:59
  • Java拷贝数组方法Arrays.copyOf()是地址传递的证明实例

    2023-11-08 11:51:29
  • Kotlin协程与并发深入全面讲解

    2022-09-11 12:28:25
  • Java中的set集合是什么意思

    2023-02-02 15:40:31
  • 深入剖析Java工厂模式让你的代码更灵活

    2022-05-26 00:42:28
  • C#中字符串的加密的源码

    2023-09-14 22:35:34
  • 解析.NET中几种Timer的使用

    2023-05-21 17:27:53
  • 使用Spring自定义实现IOC和依赖注入(注解方式)

    2023-09-16 04:42:35
  • asp之家 软件编程 m.aspxhome.com