Material Design 控件知识梳理(7) BottomNavigationBar

1、概述

当某个主页面有多个子页面时,咱们通常会采用ViewPager来承载这些子页面,并会提供一组选项卡让用户经过点击对应的选项的方式来进行页面之间的快速切换,而这一组选项卡根据摆放位置的不一样,通常能够分为下面两种实现方式:android

  • 放在顶部,采用TabLayout
  • 放在底部,采用BottomNavigationBar

今天,咱们介绍第二种方式。bash

2、BottomNavigationBar详解

2.1 基本用法

引入依赖包:app

compile 'com.ashokvarma.android:bottom-navigation-bar:1.2.0'
复制代码

在布局当中引入控件,这里,咱们将它放置在容器的底部:ide

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.demo.lizejun.repotransition.NavigationBarActivity">
    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</android.support.design.widget.CoordinatorLayout>
复制代码

在代码中对数据进行初始化:函数

private void initViews() {
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation);
        //1.设置Mode
        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        //2.设置BackgroundStyle
        mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        //3.设置背景色
        mBottomNavigationBar.setBarBackgroundColor(android.R.color.white);
        //4.设置每一个Item
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_1, "Item 1").setActiveColorResource(android.R.color.holo_blue_dark));
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_2, "Item 2").setActiveColorResource(android.R.color.holo_green_dark));
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_3, "Item 3").setActiveColorResource(android.R.color.holo_orange_dark));
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_4, "Item 4").setActiveColorResource(android.R.color.holo_green_dark));
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_5, "Item 5").setActiveColorResource(android.R.color.holo_orange_dark));
        //5.初始化
        mBottomNavigationBar.initialise();
    }
复制代码

上面代码的运行效果以下图所示: 布局

能够看到,上面咱们设置了不少的属性,下面咱们就一一来说解各个属性的含义。

2.2 BottomNavigationBarMode属性

Mode的设置对应于这句:spa

mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
复制代码

这个属性有两种可选的值,MODE_FIXEDMODE_SHIFTING3d

  • MODE_FIXED:选中的Item会稍大于未选中的Item,不管Item是否选中,都会显示文字和图标。
  • MODE_SHIFTING:选中的Item明显大于未选中的Item,未选中的Item只显示图标,而且在选中项切换的时候,会有必定的偏移效果。

2.1当中,咱们演示的就是第一种方式,下面,咱们看一下第二种方式的效果:code

mBottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
复制代码

2.3 BottomNavigationBarBackgroundStyle属性

BackgroundStyle的设置对应于这句:cdn

mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
复制代码

这个属性有两个可选的值:

  • BACKGROUND_STYLE_STATIC
  • BACKGROUND_STYLE_RIPPLE

这两种选项决定了两点:整个BottomNavigationBar的颜色被选中Item的颜色,在解释这个以前,咱们须要先了解一下三种颜色:

  • barBackgroudColor:只能经过BottomNavigationBar来设置
mBottomNavigationBar.setBarBackgroundColor(android.R.color.white);
复制代码
  • activeColor:被激活颜色,能够经过BottomNavigationBar来进行全局的设置,也能够给每一个Item单独设置
mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_1, "Item 1").setActiveColorResource(android.R.color.holo_blue_dark));
复制代码
  • inActiveColor:未被激活颜色,能够经过BottomNavigationBar来进行全局的设置,也能够给每一个Item单独设置。

注意到,上面这三种颜色并非说被选中的Item的文字和图标的颜色必定是被激活颜色,这须要根据BackgroundStyle来决定,在每种模式下,被选中Item的文字图片颜色、未被选中的Item的文字图标颜色、整个BottomNavigationBar的背景颜色的对应关系为:

也就是说, inActiveColor在任什么时候候都是未被选中 Item的文字和图片颜色,而其它两种则否则:

  • static模式下,activeColor是被选中Item的文字图标颜色,backgroundColorBottomNavigationBar的背景色
  • 而在ripple模式下,恰巧是反过来。

对于2.1中例子的Item 1,它的BackgroundStyleBACKGROUND_STYLE_STATIC,所以在它被选中的时候,文字和图片的颜色为给它设置的ActiveColor,而整个BottomNavigationBar的背景色为BackgroundColor,如今咱们看一下BACKGROUND_STYLE_RIPPLE的状况:

mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
复制代码

2.4 给Item设置角标

经过BottomNavigationItemsetBadgeItem方法,能够给每一个Item设置一个独立的角标,对于角标支持设置它的背景、文案、文案颜色以及在选中时是否隐藏角标:

BadgeItem badgeItem = new BadgeItem()
                .setBackgroundColorResource(android.R.color.holo_red_dark) //设置角标背景色
                .setText("5") //设置角标的文字
                .setTextColorResource(android.R.color.white) //设置角标文字颜色
                .setHideOnSelect(true); //在选中时是否隐藏角标
mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_5, "Item 5")
    .setActiveColorResource(android.R.color.holo_orange_dark)
    .setBadgeItem(badgeItem));
复制代码

效果以下图所示:

2.5 监听Item的切换

能够经过下面的方法来监听Item之间的切换:

mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_5, "Item 5").setActiveColorResource(android.R.color.holo_orange_dark).setBadgeItem(badgeItem));
mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {

            @Override
            public void onTabSelected(int position) {
                Log.d("onTabSelected", "position=" + position);
            }

            @Override
            public void onTabUnselected(int position) {
                Log.d("onTabUnselected", "position=" + position);

            }

            @Override
            public void onTabReselected(int position) {
                Log.d("onTabReselected", "position=" + position);

            }
});
复制代码
  • onTabSelected,某个Item从未选中状态变为选中状态时回调
  • onTabUnselected,某个Item从选中变为未选中时回调
  • onTabReselected,某个Item已经处于选中状态,可是它又被再次点击了,那么回调这个函数。

2.6 指定当前选中的位置

指定初始时刻的位置:

mBottomNavigationBar.setFirstSelectedPosition(3).initialise();
复制代码

动态改变位置:

mBottomNavigationBar.selectTab(2);
复制代码

2.7 初始化

在改变设置以后,须要在最后调用下面这句才会生效

mBottomNavigationBar.initialise();
复制代码

3、BottomNavigationBar的显示和隐藏

3.1 手动隐藏和显示BottomNavigationBar

经过下面的两个方法能够手动显示和隐藏BottomNavigationBar

public void show(View view) {
        mBottomNavigationBar.unHide(true);
    }

    public void hide(View view) {
        mBottomNavigationBar.hide(true);
    }
复制代码

3.2 根据列表的滚动来显示和隐藏

若是咱们的根布局使用的是CoordinatorLayout,那么能够经过给BottomNavigationBar设置内置的Behavior来实现动态地显示和隐藏,首先继承于这个内置的Bahavior,给它指定一个构造函数:

public class BottomBehavior extends BottomVerticalScrollBehavior<BottomNavigationBar> {

    public BottomBehavior(Context context, AttributeSet attributeSet) {
        super();
    }

}
复制代码

把这个Behavior设置给BottomNavigationBar

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.demo.lizejun.repotransition.NavigationBarActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.ashokvarma.bottomnavigation.BottomNavigationBar
        android:id="@+id/bottom_navigation"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.demo.lizejun.repotransition.behavior.BottomBehavior"/>
</android.support.design.widget.CoordinatorLayout>
复制代码

只须要这两部操做,就能够实现动态地显示和隐藏了:


更多文章,欢迎访问个人 Android 知识梳理系列:

相关文章
相关标签/搜索