Android 支持库 - 从 NavigationView 获取标题 TextView

Android Support Library - get title TextView from NavigationView

NavigationViewMenuItem 的标题可以得到 TextView 吗? (我要换字体)

drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="single">

        <item
        android:id="@+id/cards"
        android:icon="@drawable/ic_menu_cards"
        android:clickable="false"
        android:title="Cards"
        app:showAsAction="always"/>

        <item
        android:id="@+id/waiter"
        android:icon="@drawable/ic_menu_waiter"
        android:clickable="false"
        android:title="Timer Mode"
        app:showAsAction="always"/>

        <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_menu_settings"
        android:title="Settings"
        app:showAsAction="always"/>


        <item
        android:id="@+id/purchase"
        android:icon="@drawable/ic_menu_purchase"
        android:clickable="false"
        android:title="Purchase"
        app:showAsAction="always"/>

        <item
        android:id="@+id/feedback"
        android:icon="@drawable/ic_menu_feedback"
        android:clickable="false"
        android:title="Feedback"
        app:showAsAction="always"/>
    </group>
</menu>

我的布局:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <!-- Removed for simplicity -->

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:theme="@style/NavigationDrawer"
        app:headerLayout="@layout/navigation_view_header"
        app:itemTextColor="@color/white"
        app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

我看到了一些其他示例,其中将 MenuItem 转换为 TextView 应该有效,但在我的情况下它只是 returns null。

TextView title = (TextView) navigationView.getMenu().getItem(0) <-- TextView becomes null

如何访问标题以便我可以操作字体?

更多背景信息:

我通常使用 DataBindings 设置字体,但是对于 Android 支持库组件这不起作用所以我必须手动访问 TextView 并像这样设置它:

textView.setTypeface(FontCache.getInstance(textView.getContext()).get("lato_regular"));

我终于找到办法了。它不是很漂亮,但它有效。

将 MenuItems 传递给方法:

setMenuItemFont(binding.navigationView.getMenu().getItem(0));

方法如下所示:

private void setMenuItemFont(MenuItem menuItem) {
    SpannableString span = new SpannableString(menuItem.getTitle());
    span.setSpan(new CustomTypefaceSpan(FontCache.getInstance(getApplicationContext()).get("lato_regular")), 0 , span.length(),  Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    menuItem.setTitle(span);
}

Helper class 扩展了 TypefaceSpan:

package android.app.app;

import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(Typeface type) {
        super("");
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setTypeface(newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        paint.setTypeface(newType);
    }
}