阻止 BottomNavigationView 在单击时增加项目字体大小

Stops BottomNavigationView from increasing items font size on click

BottomNavigationView 的默认行为会在单击项目时增加字体大小,导致一些较长的标题被截断。

我想避免字体变大,我该怎么做?

谢谢!

使用以下键在尺寸文件中添加文本大小

 <dimen name="design_bottom_navigation_active_text_size" tools:override="true">12sp</dimen>
<dimen name="design_bottom_navigation_text_size" tools:override="true">12sp</dimen>

要消除闪烁,您必须禁用移动。

import android.annotation.SuppressLint;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.util.Log;

import java.lang.reflect.Field;

public class BottomNavigationViewHelper {
    @SuppressLint("RestrictedApi")
    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.setShifting(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("Error BottomBar", e.getLocalizedMessage());
        } catch (IllegalAccessException e) {
            Log.e("Error BottomBar", e.getLocalizedMessage());

        }
    }
}

将此 class 应用于您的导航视图

BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

现在您不需要使用反射来禁用移位。

从支持库 28.0.0 或 material 库中添加了新方法。

setLabelVisibilityMode() 检查 here.

你可以像这样使用它。

bottomNavigationView.setLabelVisibilityMode(LABEL_VISIBILITY_UNLABELED);