Google最近添加到Material design中的底部导航栏BottomNavigationBar,也可以说是现今Android底部导航栏的一个标准与统一吧。
效果图:
使用步骤:
- 在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) {
}
});
所有方法靠自己实现
BottomNavigationBar自定义
- 设置导航栏模式
Attribute: bnbMode Values: mode_default, mode_fixed, mode_shifting
Method: setMode() Values:MODE_DEFAULT, MODE_FIXED, MODE_SHIFTING
分别是属性或者代码设置
bottomNavigationBar
.setMode(BottomNavigationBar.MODE_FIXED);
- 设置导航栏背景
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)
注:模式跟背景的设置都要在添加tab前面。
- 设置颜色
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")
- 添加标记
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可以通过代码在任何时间隐藏或显示
bottomNavigationBar.hide();
bottomNavigationBar.show();
参考:
https://github.com/Ashok-Varma/BottomNavigation
BottomNavigationBar实现Android特色底部导航栏
网友评论