[英]Android Support Library - get title TextView from NavigationView
Is it possible to get a TextView
for the title of a MenuItem
for a NavigationView
? 是否可以为NavigationView
的MenuItem
的标题获取TextView
? (I want to change the font) (我想更改字体)
drawer_menu.xml 抽屉_菜单.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>
And my layout: 而我的布局:
<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>
I have seen some other example where casting the MenuItem
to TextView
should work but in my case it just returns null. 我已经看到了其他一些示例,在该示例中,应该将MenuItem
强制转换为TextView
但是在我的情况下,它仅返回null。
TextView title = (TextView) navigationView.getMenu().getItem(0) <-- TextView becomes null
How can I access the title so I can manipulate the font? 如何访问标题以操纵字体?
More Background info: 更多背景信息:
I am usually setting the font using DataBindings but for Android Support Library components this does not work so I have to access the TextView manually and set it like this: 我通常使用DataBindings设置字体,但是对于Android支持库组件来说,这是行不通的,因此我必须手动访问TextView并进行如下设置:
textView.setTypeface(FontCache.getInstance(textView.getContext()).get("lato_regular"));
I finally found a way to do it. 我终于找到了一种方法。 Its not very pretty but It works. 它不是很漂亮,但是可以工作。
Pass MenuItems to a method: 将MenuItems传递给方法:
setMenuItemFont(binding.navigationView.getMenu().getItem(0));
Method looks like this: 方法如下:
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 that extends a TypefaceSpan: 扩展TypefaceSpan的Helper类:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.