取回旧的BottomBar

时间:2017-06-21 10:32:43

标签: android android-layout bottomnavigationview

刚刚开始使用新应用,当包含BottomNavigationView结果时,结果如下:enter image description here

我真的不知道它来自何处以及如何回到仍然在spec

中定义的旧版本

有人知道这些变化的来源吗?

2 个答案:

答案 0 :(得分:1)

默认情况下BottomNavigationView在其超过3个项目时添加shiftingmode = true。所以试试这个

static void removeShiftMode(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);
            item.setShiftingMode(false);
            // set once again checked value, so view will be updated
            item.setChecked(item.getItemData().isChecked());
        }
    } catch (NoSuchFieldException e) {
        Log.e("ERROR ", "Unable to get shift mode field"+e);
    } catch (IllegalAccessException e) {
        Log.e("ERROR", "Unable to change value of shift mode"+e);
    }
}


BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

答案 1 :(得分:0)

您需要为此创建帮助程序类

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 bnav = (BottomNavigationView) findViewById(R.id.bottomnav);
 BottomNavigationViewHelper.disableShiftMode(bnav);