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>

在这里插入图片描述

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);


    }


}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42418169/article/details/117305196

智能推荐

聚类分析法-K-means聚类_k-means聚类分析_不爱写程序的程序猿的博客-程序员秘密

K-means聚类法1.简介​ 用层次聚类法聚类时,随着聚类样本对象的增多,计算量会迅速增加,而且聚类结果-谱系图会十分复杂,不便于分析,特别是样品的个数很大(如n&gt;=100)的时,层次聚类法的计算量会非常大,将占据大量的计算机内存空间和较多的计算时间.​ 为了改进上述缺点, 一个自然的想法是先粗略地分一下类,然后按某种最优原则进行修正,直到将类分得比较合理为止. 基于这种思想就产生了动态聚类法,也称逐步聚类法.​ 动态距离法适用大型数据.动态聚类法有许多种方法,这里介绍一种比较流行的动态聚类

java获取session_在Java中,request怎样取得session中的值_chen cs的博客-程序员秘密

展开全部使用request.getSession()方法获取Session, 本方法是从当前request中获取session,如果获取不到session,则会自动创建一个session,并返回新32313133353236313431303231363533e4b893e5b19e31333365656632创建的session;如果获取到,则返回获取到的session; 获取到session后...

Spring Boot集成Graylog实现微服务日志聚合、分析_lakernote的博客-程序员秘密

文章目录1.在Graylog中录入输入源2.Spring Boot集成其他常用搜索语法logback.xml多环境切换1.在Graylog中录入输入源选择System按钮中的input,录入一个输入源,如图以GELF UDP为例,在图中位置选择GELF UDP,选择完成后点击Launch new input,如图2.Spring Boot集成可在组件市场搜索logback、log4j、log4j2logback:https://github.com/osiegmar/logback-gelf

生信宝典文章集锦,一站式学习生信!众多干货,有趣有料_生信宝典的博客-程序员秘密

生信的作用越来越大,想学的人越来越多,不管是为了以后发展,还是为了解决眼下的问题。但生信学习不是一朝一夕就可以完成的事情,也许你可以很短时间学会一个交互式软件的操作,却不能看完程序教学视频...

python-mqtt自动化测试_mqttx 自动化发送_stepan.jiang的博客-程序员秘密

# -*- coding:utf-8 -*-from multiprocessing import Processimport paho.mqtt.publish as publishfrom Data.data import *import paho.mqtt.subscribe as subscribefrom common.yaml_common_method import *import time,json,yaml,os"""基础数据yaml获取"""data = yaml_me.

软件工程之需求分析②(软件需求规则说明书、数据要求说明书、初步用户手册、软件开发实施计划)_需求分析说明书_Elsa~的博客-程序员秘密

       软件需求分析阶段研究的对象是软件项目的用户要求,如何准确表达用户的要求,怎 样与用户共同明确将要开发的是一个什么样的系统,是需求分析要解决的主要问题。也就 是说需求阶段的任务并不是确定系统怎样完成工作,而仅仅是确定系统必须完成哪些工作, 即对目标系统提出完整、准确、清晰、具体的要求。需求分析阶段所要完成的任务是以软 件计划阶段确定的软件工作范围为指南,通过分析综合建立分析模型,编制出...

随便推点

机械制图和计算机编程,不懂机械制图可以学习CNC编程吗?_weixin_39662578的博客-程序员秘密

能进行合理的分类(2)对软件功,提高回忆效率如许不只可,把握软件功能的使用并且有助于从全体上。人员所要熟练控制的最根基也是最主要的一门根本课程2013-05-02展开全数机械制图是模具从业。一阶段的理论讲授部门这门课程一般放置在第,读零件图、识读拆卸图等内容包罗:尺度件、识。一样能够读模具专业所以不懂机械制图,NC编程能够读C,根基的机械制图起头可是起首必定要从。回覆的评价是?评论收已赞过已踩过你...

【vue项目部署】Linux+Nginx实现项目部署及跨域_linux nginx vue_程序媛小y的博客-程序员秘密

我们可以使用epel源(EPEL : Extra Packages for Enterprise Linux是基于Fedora的一个项目,为“红帽系”的操作系统提供额外的软件包,适用于RHEL、CentOS和Scientific Linux.)。Centos 7下安装nginx,使用yum install nginx,报错提示没有可用的软件包。在浏览器输入你的公网IP,无需端口号(默认80),如果看到。之类的界面,就是Nginx安装并启动成功了。一般,我们的项目都会放在。目录下,如果没有,创建。

【IPC通信】key_t键和ftok函数_weixin_33728268的博客-程序员秘密

为什么80%的码农都做不了架构师?&gt;&gt;&gt; ...

OPENWRT入门之一------openwrt简介_TiyaTiyaTiya的博客-程序员秘密

OPENWRT入门之一------openwrt简介     现阶段openwrt官方支持的路由列表:wiki.openwrt.org/toh/start(没有url权限,自己复制地址栏看看)     如果要学习openwrt的话,买些列表中二手的路由器来实践下更容易学习,大部分要升级一下rom芯片和内存芯片,典型配置都是4M、64M。     大家先学习一下,等待小米开放时刻的到

MongoDB介绍(资源)_weixin_33852020的博客-程序员秘密

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

MFC创建非模态对话框和销毁过程_mfc非模态对话框销毁_mailzst1的博客-程序员秘密

今天项目中遇到的问题,记录下来,做个总结。 一个简单的目的是创建一个非模态对话框并在对话框关闭后将其销毁。 这里的销毁包括:销毁对话框对象资源和对话框对象指针; 首先说创建对话框: 一、模态对话框(model dialog box) 在程序运行的过程中,若出现了模态对话框,那么主窗口将无法发送消息,直到模态对话框退出才可以发送。 点击模态对话框中的O...

推荐文章

热门文章

相关标签