How to increase width of BottomNavigationView to fill the screen

后端 未结 3 1568
时光取名叫无心
时光取名叫无心 2021-01-07 15:01

How to increase width of BottomNavigationView menu. Below is the current scenario. I am not able to increase the width to complete screen.

Menu.xml

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 15:50

    i dont remember where i found this solution exactly but you use this helper class to disable item shifting for BottomNavigation using Java Reflection :

    import android.content.Context;
    import android.graphics.Typeface;
    import android.support.design.internal.BottomNavigationItemView;
    import android.support.design.internal.BottomNavigationMenuView;
    import android.support.design.widget.BottomNavigationView;
    import android.util.Log;
    import android.widget.TextView;
    import java.lang.reflect.Field;
    
    public class BottomNavigationViewHelper {
        public static void disableShiftMode(Context context,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);
    
                    Field LargeText = item.getClass().getDeclaredField("mLargeLabel");
                    LargeText.setAccessible(true);
                    Field SmallText = item.getClass().getDeclaredField("mSmallLabel");
                    SmallText.setAccessible(true);
    
    //                 TextView SmallTextView =(TextView) SmallText.get(item);
    //                 TextView LargeTextView =(TextView) LargeText.get(item);
    
    //                 Typeface tf = TypefaceHelper.getTypeface(context);
    //                 SmallTextView.setTypeface(tf);
    //                 LargeTextView.setTypeface(tf);
    
                    //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);
            }
        }
    }
    

提交回复
热议问题