BottomNavigationView的特殊用法

来源:互联网 发布:list获取指定元素java 编辑:程序博客网 时间:2024/06/08 06:26


一、BottomNavigationView的使用规范:

1:官方建议item个数为3-5个 ,尝试6个会直接报错,尝试2个则不会报错,但是item是2个的话不建议使用改控件

2:底部导航栏的高度建议是56dp

二、给图标添加选择器效果:

直接在selector选择器中设置选中和未选中的图标样式,然后在设置该item的icon为刚设置的selector

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">       <item android:drawable="@mipmap/p1" android:state_checked="true"></item>       <item android:drawable="@mipmap/p4" ></item></selector>

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:id="@+id/item_1"        android:icon="@drawable/seletor_item1"              android:title="消息" /></menu>

三、关于BottomNavigationView的ShiftingMode

当item数大于3个的时候,效果和三个的时候完全不同了,难以接受。。。查看源码,当item数大于3的时候会开启ShiftingMode效果,并没有任何属性和方法去修改ShiftingMode,只能我们通过反射来修改:

/** * 新建一个BottomNavigationView帮助者类 */public class BottomNavigationViewHelper {    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.setShiftingMode(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);        }    }}

直接调用即可:

BottomNavigationViewHelper.disableShiftMode(mBottomNavigationView);


原创粉丝点击