Android_MaterialDesign

MaterialDesign系列文章(十六)BottomNavi

2017-11-10  本文已影响67人  笔墨Android

不怕跌倒,所以飞翔

特别说明:

BottomNavigationView 是安卓官方提供的底部导航栏,能够方便的实现下图中的底部导航效果。当导航标签少于3个的时候,也可以使用,不会报错,但实际项目中一般不会有少于三个的;如果导航标签超过5个,在使用BottomNavigationView 时在运行期会报错,错误信息如下:
java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()。意思是说,最多不能超过 5 个条目!

简单的使用

1.xml的一些标签说明:

这里贴一个简单的颜色Select和布局的Menu

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#f4b733" android:state_checked="true"></item>
    <item android:color="#666565"></item>
</selector>

<?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:icon="@drawable/ic_menu_camera"
        android:title="音乐"/>
    <item
        android:icon="@drawable/ic_menu_gallery"
        android:title="体育"/>
    <item
        android:icon="@drawable/ic_menu_manage"
        android:title="新闻"/>
    <item
        android:icon="@drawable/ic_menu_send"
        android:title="我的"/>
</menu>

2.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);
        }
    }
}

然后代码调用

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

这一系列文章的地址,希望对大家有帮助

项目地址

上一篇 下一篇

猜你喜欢

热点阅读