底部导航栏 - 如何更改菜单项字体?

6

我正在使用BottomNavigationView来管理碎片。是否有简单的解决方案可以更改选项卡项字体?我尝试使用SpannableStringBuilder,但它并没有起作用。

       for (int i = 0; i < bottomBar.getMenu().size(); i++) {
            MenuItem menuItem = binding.bottomBar.getMenu().getItem(i);
            SpannableStringBuilder title = new SpannableStringBuilder(menuItem.getTitle());
            title.setSpan(mTypeface, 0, title.length(), 0);
            menuItem.setTitle(title);
        }

1
请查看此链接:link - Fatih Santalu
是的,我看到了这个答案。谢谢你的帮助@santalu。 - yusufonderd
https://dev59.com/8VcO5IYBdhLWcg3wkiVN#53379256 - Abhinav Saxena
2个回答

10

最终,我找到了解决方案。首先,我发现了CustomTypefaceSpan类。CustomTypefaceSpan类是从TypefaceSpan类继承而来的。你可以查看这个答案

        CustomTypefaceSpan typefaceSpan = new CustomTypefaceSpan("", mTypeface);
        for (int i = 0; i <bottomBar.getMenu().size(); i++) {
            MenuItem menuItem = bottomBar.getMenu().getItem(i);
            SpannableStringBuilder spannableTitle = new SpannableStringBuilder(menuItem.getTitle());
            spannableTitle.setSpan(typefaceSpan, 0, spannableTitle.length(), 0);
            menuItem.setTitle(spannableTitle);
        }

我的代码与你的代码不兼容。这个可以用:https://dev59.com/8VcO5IYBdhLWcg3wkiVN#53379256 - Abhinav Saxena

2

我认为这很简单。覆盖 BottomNavigationView 类的 onLayout 方法,然后您就可以使用扩展标签。这还显示所有菜单标题并禁用移动。

public final class ExtendedBottomNavigationView extends BottomNavigationView{
    private final Context context;
    private Typeface fontFace = null;

    public ExtendedBottomNavigationView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
        final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
        final int bottomMenuChildCount = bottomMenu.getChildCount();
        BottomNavigationItemView item;
        View itemTitle;
        Field shiftingMode;

        if(fontFace == null){
            fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.VazirBold));
        }
        try {
            //if you want to disable shiftingMode:
            //shiftingMode is a private member variable so you have to get access to it like this:
            shiftingMode = bottomMenu.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(bottomMenu, false);
            shiftingMode.setAccessible(false);
        } catch (NoSuchFieldException e){
            e.printStackTrace();
        } catch (IllegalAccessException e){e.printStackTrace();}

        for(int i=0; i<bottomMenuChildCount; i++){
            item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
            //this shows all titles of items
            item.setChecked(true);
            //every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle
            itemTitle = item.getChildAt(1);
            //every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
            ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTypeface(fontFace, Typeface.BOLD);
            ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTypeface(fontFace, Typeface.BOLD);
        }
    }
}

然后像这样使用它:
<your.package.name.ExtendedBottomNavigationView android:id="@id/bottomMenu" style="@style/bottomMenu"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接