我正在构建一个应用程序,我们正在使用一个名为Apercu的自定义字体,您可能会听到它 . 无论如何,我已经构建了一个工具来帮助我在所有元素上设置字体 .
这就是该util中的一个方法的样子(我也有在ViewGroup上设置这个字体的方法,我循环遍历所有元素):
public static void setApercuOnTextView(final TextView view, final Context context) {
Typeface tmpTypeface = apercu;
Typeface tmpTypefaceBold = apercuBold;
if (tmpTypeface == null && tmpTypefaceBold == null) {
apercu = Typeface.createFromAsset(context.getAssets(), "fonts/Apercu.otf");
apercuBold = Typeface.createFromAsset(context.getAssets(), "fonts/Apercu-Bold.otf");
}
if (view.getTypeface() == Typeface.DEFAULT_BOLD) {
view.setTypeface(apercuBold, Typeface.BOLD);
} else if (((TextView) view).getTypeface() == Typeface.DEFAULT) {
view.setTypeface(apercu);
}
}
我的意图是我在xml中设置为粗体的所有TextView:
<TextView
android:id="@+id/name_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
但是,当我稍后在代码中运行时:
TextView name = (TextView) findViewById(R.id.name_textview);
Fonthelper.setApercuOnTextView(name, getActivity());
它没有进入if语句,我尝试获取所有粗体字体...我也尝试使用Typeface.BOLD,但它不会进入if语句 .
我目前的自定义解决方案是这样做:
TextView name = (TextView) findViewById(R.id.name_textview);
name.setTypeface(null,Typeface.BOLD);
Fonthelper.setApercuOnTextView(name, getActivity());
有没有人对此有任何线索?
1 回答
怎么样:确保你确定
view.getTypeface()
不是null
...当我outqoutedandroid:textStyle="bold"
没有找到Typeface
.