在布局中写入
ViewPager
<com.example.wanandroid.ui.fragment.ScrollableViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bnv"
android:layout_below="@+id/toolbar"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/bnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@android:color/white"
//可以设置动画样式
app:labelVisibilityMode="labeled"
//文字颜色
app:itemTextColor="@drawable/select_textcolor"
//图片颜色
app:itemIconTint="@drawable/select_textcolor"
//设置数据
app:menu="@menu/nav_menu"/>
在Xml中写入
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/home"
android:icon="@drawable/rb_shouye"
android:title="首页"
app:showAsAction="ifRoom" />
<item
android:id="@+id/guide"
android:icon="@drawable/rb_tixi"
android:title="知识体系"
app:showAsAction="ifRoom" />
<item
android:id="@+id/msg"
android:icon="@drawable/ic_wx_article"
android:title="公众号"
app:showAsAction="ifRoom" />
<item
android:id="@+id/person"
android:icon="@drawable/rb_daohang"
android:title="导航"
app:showAsAction="ifRoom" />
<item
android:id="@+id/sett"
android:icon="@drawable/rb_xiangmu"
android:title="项目"
app:showAsAction="ifRoom" />
</menu>
在代码里写入
mVp.addOnPageChangeListener (new ViewPager.OnPageChangeListener () {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
mBnv.getMenu ().getItem (i).setChecked (true);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
// 为bnv设置选择监听事件
mBnv.setOnNavigationItemSelectedListener (new BottomNavigationView.OnNavigationItemSelectedListener () {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId ()) {
case R.id.home:
// 跳转到对应的页面
mVp.setCurrentItem (0);
break;
case R.id.guide:
mVp.setCurrentItem (1);
break;
case R.id.msg:
mVp.setCurrentItem (2);
break;
case R.id.person:
mVp.setCurrentItem (3);
break;
case R.id.sett:
mVp.setCurrentItem (4);
break;
}
// 这里必须返回true才能响应点击事件
return true;
}
});
所遇问题归纳
注意 :底部Bottom选项,最少为3项,超过3项之后,BottomNavigationView会自动使用本身控件的自带动画属性,同时最多好像为5项,目前没有进行验证。
如果想要图标本身的颜色就加
mBottomNV = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mBottomNV.setItemIconTintList(null);
问题 :当底部Button超过三个之后,自带动画效果
需求 :取消原有动画效果,常规显示
解决方式 :
创建BottomNavigationViewHelper类,进行disableShiftMode方法调用
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt (0);
try {
Field shiftingMode = menuView.getClass ().getDeclaredField ("mShiftingMode");
shiftingMode.setAccessible (true);
shiftingMode.setBoolean (menuView, false);
shiftingMode.setAccessible (false);
for (int i = 0; i < menuView.getChildCount (); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt (i);
//noinspection RestrictedApi
item.setShifting (false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked (item.getItemData ().isChecked ());
}
} catch (NoSuchFieldException e) {
Log.e ("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e ("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
设置BottomNavigationView展示类型
BottomNavigationView mBv = (BottomNavigationView) findViewById(R.id.bv);
//调用上面的类进行设置
BottomNavigationViewHelper.disableShiftMode(mBv);
网友评论