如何为NavigationView中的项目设置自定义字体?

74

通过新的NavigationView,我们可以在XML中使用菜单资源设置抽屉的项目。

这样,我们可以像这样设置每个项目

<item
  android:id="@+id/drawer_my_account"
  android:icon="@drawable/ic_my_account"
  android:title="@string/drawer_my_account" />

但是现在,我想为我的抽屉中的每个项设置自定义字体,但我无法找到一种通过XML或Java代码实现的方法。是否有方法可以做到这一点?


https://dev59.com/8VcO5IYBdhLWcg3wkiVN#53379256 - Abhinav Saxena
15个回答

2
对于那些使用@Moinkhan答案的人,要将字体应用于菜单的每个部分,请使用该解决方案,并为每个标题部分使用ID。 您的菜单如下所示...
<item android:title="@string/others" android:id="@+id/nav_others">
    <menu>
        <item
            android:id="@+id/contact"
            android:title="@string/contact"/>
    </menu>
</item>

以及像这样的解决方案...

navMenu = navView.getMenu();
    MenuItem item= navView.getMenu().findItem(R.id.nav_others);
    applyFontToMenuItem(item);

也许可以帮助某些人。

1
将字体文件添加到 res/font/ 文件夹中以将字体捆绑为资源,然后可以使用样式资源更改它。在您的 styles.xml 中:
<style name="Widget.BottomNavigationView" 
   parent="Widget.Design.BottomNavigationView">
   <item name="fontFamily">@font/your_font</item>
</style>

然后在您的视图中将其应用为主题:
<android.support.design.widget.BottomNavigationView
   ...
   android:theme="@style/Widget.BottomNavigationView"
/>

0
这是另一种方法:
一个 NavigationView 有称为 NavigationMenuItemView 的子项。 NavigationMenuItemView 有两个子项,其中之一是 AppCompatCheckedTextView
像下面这样覆盖 NavigationView 的 onLayout 方法,并更改 AppCompatCheckedTextView 的 Typefase
public final class NavigationViewWithCustomFont extends NavigationView{
    private final Context context;
    private Typeface fontFace;

    public NavigationViewWithCustomFont(Context context, AttributeSet attrs){
        super(context, attrs);
        this.context = context;
        this.fontFace = null;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
        final ViewGroup navMenuView = (ViewGroup)getChildAt(0);
        final int navMenuItemsCount = navMenuView.getChildCount();
        ViewGroup itemView;

        if(fontFace == null){
            fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.BTrafficBold));
        }
        for(int i=0; i<navMenuItemsCount; i++){
            itemView = (ViewGroup)navMenuView.getChildAt(i);

            if(itemView instanceof NavigationMenuItemView ){
                CheckedTextView checkedTextView = (CheckedTextView)itemView.getChildAt(0);
                checkedTextView.setTypeface(fontFace, Typeface.BOLD);
            }
        }
    }
}

0
BottomNavigationView bottom_nav = findViewById(R.id.bottom_nav);
Typeface font = Typeface.createFromAsset(getAssets(), "--your customized font file--");
for (int i = 0; i <bottom_nav.getMenu().size(); i++) {
        MenuItem menuItem = bottom_nav.getMenu().getItem(i);
        SpannableStringBuilder spannableTitle = new SpannableStringBuilder(menuItem.getTitle());
        spannableTitle.setSpan(font.getStyle(), 0, spannableTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        menuItem.setTitle(spannableTitle);
    }

欢迎来到 Stack Overflow。请在您的回答中添加更多信息,不要仅仅把代码丢在那里。 - oscfri

-1
applyFontToMenuItem(popup.getMenu().getItem(0));
private void applyFontToMenuItem(MenuItem mi) {
    Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Redressed.ttf");       
    SpannableString mNewTitle = new SpannableString(mi.getTitle());
    mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(),pannable.SPAN_INCLUSIVE_INCLUSIVE);
    mi.setTitle(mNewTitle);
}

1
不仅要将代码直接抛出,还需要添加一些信息并对答案进行格式化。 - Satan Pandeya

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