在android中使用外部字体
我想在我的应用程序中使用外部字体。我尝试使用 AssetManager
添加新的字体
,但它不起作用。下面是我的代码:
Typeface face;
face = Typeface.createFromAsset(getAssets(), "font.otf");
textview.setTypeface(face);
但它没有显示文本...
请帮助我。
I want to use external fonts in my app. I have tried adding new fonts
using AssetManager
but it did not work. Below is my code:
Typeface face;
face = Typeface.createFromAsset(getAssets(), "font.otf");
textview.setTypeface(face);
but its not showing the text...
Please help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
AFAIK,Android 不支持 OpenType。请改用 TrueType 字体。
更新:显然现在支持 OpenType,至少在某种程度上是这样。它最初不受支持,因此您需要在应用程序支持的任何 Android 版本上彻底测试您的字体。
AFAIK, Android does not support OpenType. Use a TrueType font instead.
UPDATE: Apparently OpenType is now supported, at least somewhat. It was not supported originally, so you will want to test your font thoroughly on whatever versions of Android your app will support.
为了轻松访问我们的字体,我们需要将其与我们的应用程序捆绑在一起,以便我们的代码随后可以加载它。为此,我们在资产直接创建一个 Fonts 文件夹
这可能是您的 .xml
在您的 .java 类中编写以下代码
In order to access our font easily, we need to bundle it with our application in a way that our code can subsequently load it. To do this, we create a Fonts folder in our assets direct
This may be your .xml
Write following code in your .java class
Android 确实支持 OTF(我不确定是哪个 SDK 版本,但它肯定适用于 1.6),我使用打字机 OTF 字体有一段时间了,但渲染效果远不如我最终使用的 TTF 版本准确(通过在线字体转换器)。基线到处都是(有些字母比其他字母高出整整 2 个像素),而在 HTC Wildfire 等 LDPI 手机上,由于像素较大,该问题被大大放大。
Android does support OTF (I'm not sure from which SDK version but it definitely works with 1.6), I was using a typewriter OTF font for a while but the rendering is nowhere near as accurate as with the TTF version I ended up using (via online font converter). The baseline was all over the place (some letters were a full 2 pixels higher than others), and on LDPI phones like the HTC Wildfire the problem is greatly magnified due to the larger pixels.
我也遇到了同样的问题。我的字体在 Android 中也无法工作,但我需要它才能工作。使用字体编辑器,我将字体中的字符复制到 Android-src-2_1 中 FontSampler 示例附带的字体中。它工作得很好。
虽然我承认我的方法从知识产权的角度来看是有问题的,但我实际上并没有使用原始字体,因为所有字符都被替换了,所有对旧字体的引用也被替换了。我曾尝试“查看”两种字体的定义方式,但使所有字体变量匹配也不起作用。所以在ned中,我使用了原始字体的骨架作为新字体的模板。
I was having the same problem. My font was not working in android either but I needed it to work. Using a font editor, I copied the characters from my font into the font that comes with the FontSampler example from Android-src-2_1. It worked perfectly.
While I will admit that my method was questionable from an intellectual property point of view, I didn't actually wind up using the original font, as all of the characters were replaced and all references to the old font where replaced as well. I had tried 'looking' at the way the two fonts were defined but making all the font variables match didn't work either. So in the ned, I used a skeleton of the original font as a template for the new font.
android支持otf和ttf格式,我体验过这两种格式。
这是我用于英语和当地语言的步骤
android supports both otf and ttf formats, i experienced both of them.
this is the step i used for both english and local languages
使用 Fontinator,它支持 Booth OTF 和 TTF 字体。
它是一个 Android 库,可以轻松使用自定义字体。
https://github.com/svendvd/Fontinator
Use Fontinator it support booth OTF and TTF Fonts
It is an Android-Library make it easy, to use custom Fonts.
https://github.com/svendvd/Fontinator