在 Android 上使用自定义字体
我正在尝试加载自定义字体,如下所示:
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
我的问题是什么?我见过几个这样的例子,但没有一个适合我的情况。 提前致谢。
I'm trying to load a custom font as follows:
private Paint customFont18;
customFont18 = new Paint();
customFont18.setTextSize(18);
Typeface fontFace = Typeface.createFromAsset(getAssets(), "FONT.TTF");
customFont18.setTypeface(fontFace);
The getAssets fails, thows this:
-The method getAssets() is undefined for the type MyClass
-assetManager cannot be resolved to a variable
What is my problem? I've seen several examples like this but none works in my case.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getAssets()
是 Context 的一种方法。如果您的类不是 Activity,则需要向其中传递上下文,然后对其调用getAssets()
。getAssets()
is a method of Context. If your class is not an activity, you'll need to pass a context into it and then callgetAssets()
on that.尝试像这样改变:
Try changing like this:
使用简单的EasyFonts第三方库为您的
TextView<设置各种自定义字体/代码>。通过使用这个库,您不必担心下载字体并将其添加到资产/字体文件夹中。还有关于 Typeface 对象的创建。
简单地说:
这个库还提供了各种字体。
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:
This library also provides variety of font faces.