當某個主頁面有多個子頁面時,咱們通常會採用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>
複製代碼
只須要這兩部操做,就能夠實現動態地顯示和隱藏了: