Remove BottomNavigationView labels

前端 未结 8 720
一向
一向 2020-11-27 03:47

Google released new support library v25 with BottomNavigationView

is there any way to remove items labels ?

8条回答
  •  借酒劲吻你
    2020-11-27 04:38

    1. Set android:title=""; in menu/abc.xml

    2. Create the below helper class which is using reflection

    import android.support.design.internal.BottomNavigationMenuView;
    import android.support.design.widget.BottomNavigationView;
    import android.support.v7.widget.AppCompatImageView;
    import android.util.Log;
    import android.view.Gravity;
    import android.widget.FrameLayout;
    
    import java.lang.reflect.Field;
    
    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);
                    item.setPadding(0, 15, 0, 0);
                    // 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);
            }
        }
    } 
    

    3. In your main activity, add these lines:

    mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
    BottomNavigationViewHelper.disableShiftMode(mBottomNav);
    

提交回复
热议问题