当某个主页面有多个子页面时,咱们通常会采用ViewPager
来承载这些子页面,并会提供一组选项卡让用户经过点击对应的选项的方式来进行页面之间的快速切换,而这一组选项卡根据摆放位置的不一样,通常能够分为下面两种实现方式:android
TabLayout
BottomNavigationBar
今天,咱们介绍第二种方式。bash
BottomNavigationBar
详解引入依赖包: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();
}
复制代码
上面代码的运行效果以下图所示: 布局
BottomNavigationBar
的Mode
属性Mode
的设置对应于这句:spa
mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
复制代码
这个属性有两种可选的值,MODE_FIXED
和MODE_SHIFTING
3d
MODE_FIXED
:选中的Item
会稍大于未选中的Item
,不管Item
是否选中,都会显示文字和图标。MODE_SHIFTING
:选中的Item
明显大于未选中的Item
,未选中的Item
只显示图标,而且在选中项切换的时候,会有必定的偏移效果。在2.1
当中,咱们演示的就是第一种方式,下面,咱们看一下第二种方式的效果:code
mBottomNavigationBar.setMode(BottomNavigationBar.MODE_SHIFTING);
复制代码
BottomNavigationBar
的BackgroundStyle
属性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
的文字图标颜色,backgroundColor
为BottomNavigationBar
的背景色ripple
模式下,恰巧是反过来。对于2.1
中例子的Item 1
,它的BackgroundStyle
为BACKGROUND_STYLE_STATIC
,所以在它被选中的时候,文字和图片的颜色为给它设置的ActiveColor
,而整个BottomNavigationBar
的背景色为BackgroundColor
,如今咱们看一下BACKGROUND_STYLE_RIPPLE
的状况:
mBottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
复制代码
Item
设置角标经过BottomNavigationItem
的setBadgeItem
方法,能够给每一个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));
复制代码
效果以下图所示:
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
已经处于选中状态,可是它又被再次点击了,那么回调这个函数。指定初始时刻的位置:
mBottomNavigationBar.setFirstSelectedPosition(3).initialise();
复制代码
动态改变位置:
mBottomNavigationBar.selectTab(2);
复制代码
在改变设置以后,须要在最后调用下面这句才会生效
mBottomNavigationBar.initialise();
复制代码
BottomNavigationBar
的显示和隐藏BottomNavigationBar
经过下面的两个方法能够手动显示和隐藏BottomNavigationBar
:
public void show(View view) {
mBottomNavigationBar.unHide(true);
}
public void hide(View view) {
mBottomNavigationBar.hide(true);
}
复制代码
若是咱们的根布局使用的是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>
复制代码
只须要这两部操做,就能够实现动态地显示和隐藏了: