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

时间:2019-06-10 11:40:42

标签: android kotlin tabs font-size bottomnavigationview

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

我想避免字体大小增加,怎么办?

谢谢!

2 个答案:

答案 0 :(得分:1)

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

 <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());

        }
    }
}

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

BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

答案 1 :(得分:0)

现在,您无需使用反射来禁用移位。

从支持库28.0.0或材料库中,添加了新方法。

setLabelVisibilityMode()选中here

您可以像使用它。

bottomNavigationView.setLabelVisibilityMode(LABEL_VISIBILITY_UNLABELED);