BottomNavigationView笔记

BottomNavigationView笔记

BottomNavigationView是一个google官方推出的底部导航栏的控件,是在2014年推出的,在design包中.但是在使用的时候发现在2018年的时候就停止更新到,迁移到了material包中,所以小伙伴们在使用的时候可以直接去material就可以了

基本使用

第一步:导入引用

在maven中的地址

https://mvnrepository.com/artifact/com.android.support/design             // 到2018年就停止更新了
https://mvnrepository.com/artifact/com.google.android.material/material   //从2018年开始更新

所以要早最新版的包还是去material中看看吧

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'

第二步:创建菜单目录

res目录下新建menu目录,并在目录下新建一个bottom_nav_menu.xml(名字自定义)文件,在此文件中加入需要的菜单项

代码如下:

<?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/ic_home_black_24dp"
        android:title="首页" />

    <item
        android:id="@+id/navigation_time"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="时刻" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="通知" />

</menu>

菜单中设置了每个菜单的标题以及图标

第三步:在页面中使用底部导航栏组件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <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" />

</androidx.constraintlayout.widget.ConstraintLayout>
属性说明

android:layout_marginStart指的是底部导航栏距离左边的边距

android:layout_marginEnd指的是底部导航栏距离右边的边距

app:itemTextColor 指的是导航栏文字的颜色

app:itemIconTint 指的是导航栏中图片的颜色

app:iteamBackground 指的是底部导航栏的背景颜色,默认是主题的颜色

app:menu 指的是布局(文字和图片都写在这个里面)

第四步:在页面中使用

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //找到底部导航栏组件
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.nav_view)
        //设置选中事件
        bottomNavigationView.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.navigation_home -> {
                    Toast.makeText(MainActivity@ this, "首页被点击了", Toast.LENGTH_SHORT).show()
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigation_time -> {
                    Toast.makeText(this, "时刻被点击了", Toast.LENGTH_SHORT).show()
                    return@setOnNavigationItemSelectedListener true
                }
                R.id.navigation_notifications -> {
                    Toast.makeText(this, "通知被点击了", Toast.LENGTH_SHORT)
                        .show()
                    return@setOnNavigationItemSelectedListener true
                }
                else -> {
                    Toast.makeText(this, "其他的组件被点击了", Toast.LENGTH_SHORT).show()
                    return@setOnNavigationItemSelectedListener true
                }
            }
        }
    }
}

配合Navigation使用

与基本使用一二三步是一样的,重复后在来到这里开始

第一步引用Navigation

    //引入navigation
    def navigationVersion = '2.3.5'
    implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"
    implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"

第二步:创建每个菜单项对应的Fragment

这里没什么好多的,就是三个fragment

第三步:编写导航菜单

  1. 在资源目录res目录下新建navigation目录

  2. navigation目录下新建导航指引图表nav_graph_main.xml(名字可自定义)文件
    文件内容如下

    <?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" android:id="@+id/nav_graph_main">
    
    </navigation>
    

第四步:将需要导航的fragment加入此导航菜单

直接上代码

nav_graph_main.xml代码如下:

<?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/nav_graph_main"
    app:startDestination="@id/navigation_home">
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.fengshan.bottomnavigationviewdemo.Fragment1"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_1">

    </fragment>
    <fragment
        android:id="@+id/navigation_time"
        android:name="com.fengshan.bottomnavigationviewdemo.Fragment2"
        android:label="@string/title_time"
        tools:layout="@layout/fragment_2">

    </fragment>
    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.fengshan.bottomnavigationviewdemo.Fragment3"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_3"></fragment>

</navigation>

第五步:在主页面中配置此菜单

activity_main.xml代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <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
        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/nav_graph_main" />

</androidx.constraintlayout.widget.ConstraintLayout>

第六步:在代码中关联并引用

MainActivity文件中的代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //找到底部导航栏组件
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.nav_view)
        //初始化navigation
        val navController = findNavController(R.id.nav_host_fragment)

        //初始化app头部Bar
        val appBarConfiguration = AppBarConfiguration(setOf(
            R.id.navigation_home, R.id.navigation_time, R.id.navigation_notifications))
        //设置头部bar随控制器切换标题
        setupActionBarWithNavController(navController,appBarConfiguration)

        //将底部导航栏组件与控制器关联起来
        bottomNavigationView.setupWithNavController(navController)
    }
}

项目地址

https://gitee.com/jdboy/bottom-navigation-view-demo.git
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值