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

智能推荐

UVA - 264 - Count on Cantor (Cantor的数表!)-程序员宅基地

文章浏览阅读1.1k次。UVA - 264Count on CantorTime Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %lluSubmit StatusDescription Count on Cantor _count on cantor

Reducing the Dimensionality of Data with Neural Networks【论文翻译】-程序员宅基地

文章浏览阅读1k次。Reducing the Dimensionality of Data with Neural Networks利用神经网络降低数据的维度G. E. Hinton* and R. R. SalakhutdinovHigh-dimensional data can be converted to low-dimensional codes by training a multilayer neural network with a small central layer to reconstruct h_reducing the dimensionality of data with neural networks

linux离线安装mysql5.7.18_liunx gtid table is not ready to be used. table 'm-程序员宅基地

文章浏览阅读652次。centos7 使用tar 离线安装mysql5.7_liunx gtid table is not ready to be used. table 'mysql.gtid_executed' cannot

RAC详细介绍-程序员宅基地

文章浏览阅读2.8k次。Oracle RAC的优势在于利用多个节点(数据库实例)组成一个数据库,这样在保证了数据库高可用性的情况下更充分的利用了多个主机的性能,而且可以通过增加节点进行性能的扩展。实现Oracle RAC需要解决的关键问题就是多节点进行数据访问时如何保证数据的一致性,Oracle是通过各节点间的私有连接进行内存融合(cache fusion)来保证各节点数据访问的一致性。用一个例子来解释一下内存融_rac

android 日志框架 github,Android日志框架LogUtils-程序员宅基地

文章浏览阅读594次。1. Features支持直接打印数据集合, 如List、Set、Map、数组等全局配置log输出, 个性化设置Tag准确显示调用方法、行,快速定位日志所在文件位置支持android系统复杂对象Intent、Bundle、Message等打印提供空实现 release-no-op版本支持高性能日志写入文件(基于mmap)兼容Android Studio 3.1 日志格式2. screenshot日..._com.apkfuns.log2file

关于nth-child的用法_ntd-child-程序员宅基地

文章浏览阅读181次。<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Do_ntd-child

随便推点

tablayout的使用-程序员宅基地

文章浏览阅读181次。使用tablayout+viewpage+fragment实现滑动页面,参考原文: http://blog.csdn.net/chenguang79/article/details/488041251. 布局文件中添加控件开始之前之前要先添加依赖库compile 'com.android.support:design:25.0.0'添加控件

android开发模式LiveData+ViewModel+Room+Retrofit_android retrofit viewmodel-程序员宅基地

文章浏览阅读6.5k次,点赞3次,收藏7次。导依赖implementation 'com.android.support:cardview-v7:26.1.0'implementation "android.arch.lifecycle:extensions:1.0.0"implementation "android.arch.persistence.room:runtime:1.0.0"annotationProcessor "an..._android retrofit viewmodel

VMware vCenter Server 的内部版本号和版本 (2143838)--2020-10-27 更新_vcenter 21784236-程序员宅基地

文章浏览阅读1.7k次。VMware vCenter Server 的内部版本号和版本 (2143838)--2020-10-27 更新Click here to view full documenthttps://kb.vmware.com/articleview?docid=2143838&lang=zh_CNSymptoms 免责声明:本文是Build numbers and versions of VMware vCenter Server (2143838)的翻译版本。尽管我们会不断努..._vcenter 21784236

SpringCloudAlibabaNacos集成阿里云OSS对象存储服务_nacos如何配置oss-程序员宅基地

文章浏览阅读895次。SpringCloudAlibaba整合oss官网示例:https://github.com/alibaba/aliyun-spring-boot/tree/master/aliyun-spring-boot-samples/aliyun-oss-spring-boot-sample首先肯定是开通oss服务了(免费的):然后创建一个bucket:然后新建一个module,作为专门处理第三方服务的微服务:引入依赖:<?xml version="1.0" encoding="UTF-8"?_nacos如何配置oss

vnc和anaconda冲突解决方法:could not make bus activated clients aware of XDG_CURRENT_DESKTOP=GNOME envi-程序员宅基地

文章浏览阅读7.7k次,点赞3次,收藏8次。1、注释掉~/.bashrc中anaconda相关的环境变量设置2、执行source ~/.bashrc3、重启一个xshell连接,启动vnc4、还原~/.bashrc中注释掉的anaconda相关的环境变量5、执行source ~/.bashrc_could not make bus activated clients aware of xdg_current_desktop=gnome envi

推荐文章

热门文章

相关标签