【发布时间】:2017-08-31 05:55:28
【问题描述】:
下面的代码是使用 trebuchet 字体的自定义 textview 类。
public class TrebuchetTextView extends TextView {
public TrebuchetTextView(Context context) {
super(context);
init();
}
public TrebuchetTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TrebuchetTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
if (!isInEditMode()) {
final Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), getContext().getString(R.string.normal_font_path));
setTypeface(typeface);
}
}
}
这在其他应用程序中使用并且效果很好。
但是当我在我的应用程序中使用自定义文本视图时会发生膨胀错误。 我在 assets/fonts 文件夹中包含了 trebuchet 字体。
【问题讨论】:
-
用问题分享你的错误
-
如果你只是想改变app的所有TextView的字体,你可以使用另一种方法。
-
在 xml 文件中使用带有完整包路径的自定义文本视图。这也必须是
getContext().getResources().getString(R.string.normal_font_path) -
the inflation error occurs那么你应该修复它。但是由于您没有说出哪个错误,所以没有显示它的堆栈跟踪 - 祝您自己修复它好运。