【问题标题】:BottomNavigationView - How to change menu item font?BottomNavigationView - 如何更改菜单项字体?
【发布时间】:2017-07-17 12:27:25
【问题描述】:

我正在使用 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);
        }

【问题讨论】:

标签: android bottomnavigationview


【解决方案1】:

终于找到了解决办法。首先我找到了 CustonTypefaceSpan 类。 CustomTypefaceSpan 扩展自 TypefaceSpan 类。您可以查看this answer

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

【讨论】:

【解决方案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"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2023-02-08
    • 2017-05-18
    • 1970-01-01
    相关资源
    最近更新 更多