我正在尝试加载自定义字体,如下所示:
private Paint customFont18;
customFont18 = new Paint();
customFont18.setTextSize(18);
Typeface fontFace = Typeface.createFromAsset(getAssets(), "FONT.TTF");
customFont18.setTypeface(fontFace);
getAssets失败了,发生了这个:
-The method getAssets() is undefined for the type MyClass
-assetManager cannot be resolved to a variable
我的问题是什么?我见过几个这样的例子,但在我的情况下都没有。 提前谢谢。
答案 0 :(得分:11)
getAssets()
是Context的一种方法。如果您的课程不是某项活动,则需要将相关内容传递给该课程,然后在其上调用getAssets()
。
public myClass(Context myContext) {
Typeface typeface = Typeface.createFromAsset(myContext.getAssets(), "FONT.TTF");
...
}
答案 1 :(得分:0)
尝试改变如下:
Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/FONT.TTF");
答案 2 :(得分:0)
Use simple EasyFonts third party library to set variety of custom font to your TextView
. By using this library you should not have to worry about downloading and adding fonts into the assets/fonts folder. Also about Typeface object creation.
Simply:
TextView myTextView = (TextView)findViewById(R.id.myTextView);
myTextView.setTypeface(EasyFonts.robotoThin(this));
This library also provides variety of font faces.