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

智能推荐

数据结构之哈夫曼树-程序员宅基地

文章浏览阅读1.6k次。哈夫曼树1.1基本介绍给定n个权值作为n个叶子结点,构造一棵二叉树,若该树的带权路径长度(wpl)达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree), 还有的书翻译为霍夫曼树。赫夫曼树是带权路径长度最短的树,权值较大的结点离根较近。1.2赫夫曼树几个重要概念和举例说明路径和路径长度:在一棵树中,从一个结点往下可..._数据结构简单的哈夫曼树

如何复制百度文库中需要收费的文字_百度文库怎么复制付费文字-程序员宅基地

文章浏览阅读2.6w次,点赞8次,收藏26次。如何复制百度文库中需要收费的文字通常我们在文库中复制需要成为VIP如:现在我们只需要按F12然后再按F1往最下划把Disable JavaScript 勾选中,注意先不要急着叉掉(把F12叉掉就不可以复制粘贴了),不要管这个界面,然后直接去左边复制粘贴就可以了。..._百度文库怎么复制付费文字

步进电机T型和S型速度曲线_步进电机速度曲线-程序员宅基地

文章浏览阅读6.7k次,点赞7次,收藏19次。一种用于步进电机加速度的新算法可以实现速度曲线的实时参数化和计算。该算法可以在低端微控制器上运行,只使用简单的定点算术运算并且不使用数据表。它以恒定的加速度和减速度形成线性斜坡时间的准确近似值。通常认为,步进电机线性速度斜坡的时间过于复杂,无法实时计算。步骤延迟的精确公式如公式8所示。解决方案是将预加坡数据存储在预编译数组中,但此方法不灵活并浪费存储器。另一种选择是使用功能更强大,处理成..._步进电机速度曲线

微信小程序开发实战2 微信小程序编程基础-程序员宅基地

文章浏览阅读3.9k次,点赞4次,收藏33次。2.微信小程序编程基础2.1小程序目录结构小程序包含一个描述整体程序的主体部分和多个小程序页面。一个小程序主体部分由三个文件组成,必须放在项目的根目录,如下:文件作用app.js小程序的入口文件app.json小程序公共配置app.wxss小程序公共样式表表 2-1 小程序主体文件一个小程序页面由四个文件组成,分别是:<文件作用>js页面逻辑文件wxml页面描述文件,用来设计页面布局,进行数据绑定等。jso_小程序编程

SystemVerilog functional coverage 学习_function coverage-程序员宅基地

文章浏览阅读5.9k次,点赞47次,收藏74次。SystemVerilog functional coverage 学习前言基于《IEEE Standard for SystemVerilog — Unified Hardware Design, Specification, and Verification Language》19章的学习和自己的理解。有不对的地方希望大家补充。 编译工具 Cadence的Xcelium, coverage收集工具是IMcOverview标准上对Func_coverage的定义是:Functional cove_function coverage

oracle imp ora-06512,记一次ORA-06512、ORA-29283-程序员宅基地

文章浏览阅读889次。一、背景简介上午应用找来,给了俩错误,错误信息如下ORA-06512: 在 "SYS.UTL_FILE", line 536ORA-29283: 文件操作无效以上报错简单来说,就是调用 sys.utl_file 输出文件,但是输出的目录不存在,进一步沟通发现,用户输出的目录是一个挂载的共享的 windows 文件夹,没错,是在 linux 上挂了一个 windows 的文件夹。二、解决过程首先尝试..._ora-06512: 在 "sys.utl_raw", line 224

随便推点

UE4蓝图:自定义鼠标操作._ue4 left mouse button-程序员宅基地

文章浏览阅读1w次,点赞3次,收藏18次。之前提到过一些基本的键盘按键事件,以及用Is Input Key Down节点来判断指定节点是否被按下.这篇博客主要是一些关于鼠标事件的基本处理.首先我们在地图编辑器中拖出一个Box.先把Box设置为静态模型,并且修改为可移动的属性,然后选中他之后进入关卡编辑器.右键获得Box的节点.不知道怎么修改的可以到http://blog.csdn.net/qq_37233607/article/det..._ue4 left mouse button

最近三年收藏网站,做一次云备份_daofire最新网址-程序员宅基地

文章浏览阅读2.1k次。这些 URL 还是要留存下来的好,万一阿里云宕机了,还能有个地方恢复。React-China中文社区http://react-china.org/top暂时水平还比较低,但还是可以看看2016-07-03 11:51:04Amazeui-Reacthttp://amazeui.org/react/getting-started实践证明一个比较适合国人口味的React组件..._daofire最新网址

电池保护板电路原理_锂电池保护板无输出激活-程序员宅基地

文章浏览阅读9.4k次,点赞4次,收藏27次。  锂元素在元素周期表中处于第三位,外层只有一个电子,是一种非常活泼的金属,而锂离子电池具有放电电流大,内阻低,寿命长,无记忆效应等被人们广泛应用,锂离子电池在使用时严禁出现过充电、过放电、以及短路等问题,否则将会使得电池出现起火或者爆炸等问题。所以在锂电池电路中通常都会增加一个保护板电路来保护锂离子电池的安全。锂电池保护板的作用  **电池保护板通常有如下几个作用:过充、过放、过流、短路以及高温保护。**上述的几个作用也是由锂电池本身的材料决定的。电池保护板通常有保护电路板和PTC等器件组成。  保_锂电池保护板无输出激活

大数据如何改变DBA工作模式_数睿通的开发dba需要改哪里-程序员宅基地

文章浏览阅读1.1k次。技术支持团队通常是支持熟悉的软硬件配置。在操作系统和数据库管理软件上特定组合的专业化是很常见的,而且这也允许某些团队成员在一家企业的IT环境中获得极为有价值的深层经验。大数据是如何改变这种模式的呢?数据库支持团队 技术支持团队的目标之一就是要与管理层协作来把他们的工作区分出优先次序。管理层提出战略规划,团队将之转化为所需时间和资源的可估计任务,然后他们共同协..._数睿通的开发dba需要改哪里

jeb2 demo keygen.md-程序员宅基地

文章浏览阅读181次。如果demo版本过期了,可以尝试用这个keygenimport java.io.*;import java.nio.ByteBuffer;import java.nio.ByteOrder;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.u..._jeb提示订阅过期

C++ STL vector:sizeof(vector)_c++ vector sizeof-程序员宅基地

文章浏览阅读1.3w次,点赞9次,收藏15次。int的大小是4,定义vector vec,vec中有一个元素,sizeof(vec)=20,如果有1000个元素,则sizeof(vec)是多少?#include #include using namespace std;int main(){ vector vec; for(int i=0;i<100;i++) { vec.push_back(i); cou_c++ vector sizeof

推荐文章

热门文章

相关标签