转发 这篇文章有角标的使用方式
使用三方BottomNavigationBar
gradle
implementation 'com.ashokvarma.android:bottom-navigation-bar:2.1.0'
xml
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bnb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@id/bvn"
app:layout_constraintStart_toStartOf="parent"
app:bnbActiveColor="#3A97FF"
app:bnbInactiveColor="#888888"
app:bnbBackgroundStyle="background_style_static"
app:bnbMode="mode_fixed"
/>
使用fixed模式实现简单的bottombar,注意fixed_height和bottom_navigation_height需要同时设置。
fixed_label_active_to_inactive_ratio = 1是不进行icon缩放
dimens.xml
<!--bottomNavigationTab-->
<dimen name="bottom_navigation_height">49dp</dimen>
<dimen name="fixed_height">49dp</dimen>
<dimen name="fixed_height_top_padding_inactive">0dp</dimen>
<dimen name="fixed_height_top_padding_active">0dp</dimen>
<dimen name="fixed_height_bottom_padding">4dp</dimen>
<dimen name="fixed_label_active">10sp</dimen>
<dimen name="fixed_label_inactive">10sp</dimen>
<item name="fixed_label_active_to_inactive_ratio" format="float" type="dimen">1</item>
Activity
mBinding.bnb.addItem(new BottomNavigationItem(R.mipmap.tab_home_sec,R.string.mainpage).setInactiveIconResource(R.mipmap.tab_home_nor))
.addItem(new BottomNavigationItem(R.mipmap.tab_find_sec,R.string.found).setInactiveIconResource(R.mipmap.tab_find_nor))
.addItem(new BottomNavigationItem(R.mipmap.tab_news_sec,R.string.message).setInactiveIconResource(R.mipmap.tab_news_nor))
.addItem(new BottomNavigationItem(R.mipmap.tab_my_sec,R.string.mine).setInactiveIconResource(R.mipmap.tab_my_nor)).initialise();
mBinding.bnb.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
@Override
public void onTabSelected(int position) {
switch (position){
case 0:
mBinding.vp.setCurrentItem(0);
break;
case 1:
mBinding.vp.setCurrentItem(1);
break;
case 2:
mBinding.vp.setCurrentItem(2);
break;
case 3:
mBinding.vp.setCurrentItem(3);
break;
}
}
@Override
public void onTabUnselected(int position) {
}
@Override
public void onTabReselected(int position) {
}
});
mBinding.vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
//将滑动到的页面对应的 menu 设置为选中状态
mBinding.bnb.selectTab(i);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});