我使用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);
}
答案 0 :(得分:8)
最后我找到了解决方案。首先我找到了CustonTypefaceSpan类。 CustomTypefaceSpan从TypefaceSpan类扩展而来。您可以查看this answer。
Thread.currentThread().sleep(3000); // 3 seconds wait
答案 1 :(得分:2)
我认为这很简单。覆盖 BottomNavigationView 类的 onLayout 方法,然后您可以使用扩展标记。这也显示所有菜单标题并禁用转换。
malloc()
然后像这样使用它:
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);
}
}
}