BottomNavigationView - How to uncheck all MenuItems and keep Titles being displayed?

后端 未结 10 1340
面向向阳花
面向向阳花 2020-12-18 19:33

As I liked the design from BottomNavigationView I decided to implement a new Menu for my App with it, instead of just using simple buttons.

I took this

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-18 20:08

    This is the same as the accepted answer except I have changed two lines of code marked below. When looping through the BottomNavigationItemViews, I set checked to false always and I also set checkable to false. This prevents the Menu Items from changing size. You still need this proguard rule:

          -keepclassmembers class android.support.design.internal.BottomNavigationMenuView {
             boolean mShiftingMode;
          }
    

    Updated code:

        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);
                    item.setChecked(false); // <--- This line changed
                    item.setCheckable(false);  // <-- This line was added
                }
            }
            catch (NoSuchFieldException e)
            {
                Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field");
            }
            catch (IllegalAccessException e)
            {
                Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode");
            }
    
       }
    

提交回复
热议问题