在android中使用外部字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1426244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use external fonts in android
提问by mudit
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:
我想在我的应用程序中使用外部字体。我试过添加新的fonts
使用,AssetManager
但没有用。下面是我的代码:
Typeface face;
face = Typeface.createFromAsset(getAssets(), "font.otf");
textview.setTypeface(face);
but its not showing the text...
但它没有显示文本...
Please help me with this.
请帮我解决一下这个。
回答by CommonsWare
AFAIK, Android does not support OpenType. Use a TrueType font instead.
AFAIK,Android 不支持 OpenType。请改用 TrueType 字体。
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.
更新:显然现在支持 OpenType,至少在某种程度上是支持的。它最初不受支持,因此您需要在您的应用支持的任何 Android 版本上彻底测试您的字体。
回答by Kirtikumar A.
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
为了轻松访问我们的字体,我们需要将它与我们的应用程序捆绑在一起,以便我们的代码可以随后加载它。为此,我们在我们的资产中直接创建一个 Fonts 文件夹
This may be your .xml
这可能是您的 .xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/DefaultFontText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Here is some text." />
<TextView
android:id="@+id/CustomFontText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="Here is some text.">
</TextView>
Write following code in your .java class
在您的 .java 类中编写以下代码
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
回答by richardleggett
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 确实支持 OTF(我不确定是哪个 SDK 版本,但它肯定适用于 1.6),我使用打字机 OTF 字体有一段时间了,但渲染远不及我最终使用的 TTF 版本准确(通过在线字体转换器)。基线到处都是(有些字母比其他字母高出整整 2 个像素),而在像 HTC Wildfire 这样的 LDPI 手机上,由于像素较大,问题被大大放大了。
回答by dave evartt
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.
我遇到了同样的问题。我的字体也不能在 android 中工作,但我需要它才能工作。使用字体编辑器,我将字体中的字符复制到 Android-src-2_1 中 FontSampler 示例附带的字体中。它工作得很好。
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.
虽然我承认我的方法从知识产权的角度来看是有问题的,但我实际上并没有使用原始字体,因为所有字符都被替换了,并且所有对旧字体的引用也被替换了。我曾尝试“查看”两种字体的定义方式,但使所有字体变量匹配也不起作用。所以在 ned 中,我使用了原始字体的骨架作为新字体的模板。
回答by deepan
android supports both otf and ttf formats, i experienced both of them.
android 支持 otf 和 ttf 两种格式,我都体验过。
tv3 = (TextView)findViewById(R.id.tv1);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/TRAJANPRO-BOLD.OTF");
tv3.setTypeface(typeFace);
this is the step i used for both english and local languages
这是我用于英语和本地语言的步骤
回答by Sven N?hler
Use Fontinator it support booth OTF and TTF Fonts
使用 Fontinator 它支持展位 OTF 和 TTF 字体
It is an Android-Library make it easy, to use custom Fonts.
它是一个 Android 库,可以轻松使用自定义字体。