2017-07-17 8 views
2

私はフラグメントを管理するためにBottomNavigationViewを使用しています。変更タブ項目のフォントのための簡単なソリューションはありますか? SpannableStringBuilderを使用しました。しかし、それは動作していません。BottomNavigationView - メニュー項目のフォントを変更するには?

 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

を参照してください[リンク](https://stackoverflow.com/a/10741161/1512199) – santalu

+0

はい、私はこの答えを見ました。助けてくれてありがとう@santalu – sorunluadam

答えて

4

最後に解決策が見つかりました。最初にCustonTypefaceSpanクラスが見つかりました。 TypeTypeSpanクラスから拡張されたCustomTypefaceSpan。 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); 
     } 
0

これは簡単だと思います。オーバーライドonLayoutメソッドBottomNavigationViewクラスの場合、拡張タグを使用できます。これはまた、すべてのメニュータイトルを表示し、シフトを無効にします。

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"/> 
関連する問題