Google最近添加到Material design中的底部導(dǎo)航欄BottomNavigationBar择卦,也可以說是現(xiàn)今Android底部導(dǎo)航欄的一個(gè)標(biāo)準(zhǔn)與統(tǒng)一吧寝受。
效果圖:
使用步驟:
- 在Gradle添加:
compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0'
- 在你的activity xml :
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:layout_gravity="bottom"
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- 在你要使用的class中
BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
bottomNavigationBar
.addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home"))
.addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books"))
.addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music"))
.addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV"))
.addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games"))
.initialise();
- 添加TabSelectedListener
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
@Override public void onTabSelected(int position) {
}
@Override public void onTabUnselected(int position) {
}
@Override public void onTabReselected(int position) {
}
});
所有方法靠自己實(shí)現(xiàn)
BottomNavigationBar自定義
- 設(shè)置導(dǎo)航欄模式
Attribute: bnbMode Values: mode_default, mode_fixed, mode_shifting
Method: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING
分別是屬性或者代碼設(shè)置
bottomNavigationBar
.setMode(BottomNavigationBar.MODE_FIXED)歇攻;
- 設(shè)置導(dǎo)航欄背景
Attribute: bnbBackgroundStyle
Values: background_style_default, background_style_static, background_style_ripple
Method: setBackgroundStyle()
Values: BACKGROUND_STYLE_DEFAULT, BACKGROUND_STYLE_STATIC, BACKGROUND_STYLE_RIPPLE
bottomNavigationBar
.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
注:模式跟背景的設(shè)置都要在添加tab前面厨疙。
- 設(shè)置顏色
Attributes: bnbActiveColor, bnbInactiveColor, bnbBackgroundColor Value: Color value or resourceMethods: setActiveColor, setInActiveColor, setBarBackgroundColor Value: Color value or resource
bottomNavigationBar
.setActiveColor(R.color.primary)
.setInActiveColor("#FFFFFF")
.setBarBackgroundColor("#ECECEC")
- 添加標(biāo)記
BadgeItem numberBadgeItem = new BadgeItem()
.setBorderWidth(4) .
.setBackgroundColorResource(R.color.blue)
.setText("5")
.setHideOnSelect(autoHide.isChecked());
bottomNavigationBar .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home")
.setActiveColorResource(R.color.orange)
.setBadgeItem(numberBadgeItem))
隱藏/顯示
BottomNavigatinoBar可以通過代碼在任何時(shí)間隱藏或顯示
bottomNavigationBar.hide();
bottomNavigationBar.show();
參考:
https://github.com/Ashok-Varma/BottomNavigation
BottomNavigationBar實(shí)現(xiàn)Android特色底部導(dǎo)航欄