我想在我的应用中使用外部字体。我尝试使用 AssetManager 添加新的 fonts ,但它不起作用。以下是我的代码:

Typeface face;

face = Typeface.createFromAsset(getAssets(), "font.otf");

textview.setTypeface(face);

但它没有显示文字......

请帮助我。

有帮助吗?

解决方案

AFAIK,Android不支持OpenType。改为使用TrueType字体。


UPDATE:现在显然支持OpenType,至少在某种程度上是支持。它最初不受支持,因此您需要在您的应用支持的任何Android版本上彻底测试您的字体。

其他提示

为了轻松访问我们的字体,我们需要将它与我们的应用程序捆绑在一起,以便我们的代码可以随后加载它。为此,我们在资产直接

中创建了一个Fonts文件夹

这可能是您的.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>

在.java类中编写以下代码

Typeface tf = Typeface.createFromAsset(getAssets(),
            "fonts/BPreplay.otf");
    TextView tv = (TextView) findViewById(R.id.CustomFontText);
    tv.setTypeface(tf);

Android确实支持OTF(我不确定从哪个SDK版本但它肯定适用于1.6),我使用打字机OTF字体已经有一段时间了,但是渲染远不如TTF版本那么准确我结束了使用(通过在线字体转换器)。基线遍布整个地方(有些字母比其他字母高出2个像素),而在像HTC Wildfire这样的LDPI手机上,由于像素较大,问题会大大放大。

我遇到了同样的问题。我的字体在android中也不起作用,但我需要它才能工作。使用字体编辑器,我将字体中的字符复制到Android-src-2_1中FontSampler示例附带的字体中。它运作得很好。

虽然我承认我的方法从知识产权的角度来看是有问题的,但我实际上并没有使用原始字体,因为所有的字符都被替换了,并且旧字体的所有引用都被替换了。我曾尝试“查看”两种字体的定义方式但是使所有字体变量匹配也不起作用。所以在我看来,我使用原始字体的骨架作为新字体的模板。

android支持otf和ttf格式,我经历过这两种格式。

tv3 = (TextView)findViewById(R.id.tv1);
    Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/TRAJANPRO-BOLD.OTF");
    tv3.setTypeface(typeFace);

这是我用于英语和当地语言的步骤

使用Fontinator支持展位OTF和TTF字体

这是一个Android-Library,可以轻松使用自定义字体。

https://github.com/svendvd/Fontinator

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top