在android中使用外部字体

mud*_*dit 60 fonts android android-widget

我想在我的应用程序中使用外部字体.我尝试添加新的fonts使用,AssetManager但它没有用.以下是我的代码:

Typeface face;

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

textview.setTypeface(face);
Run Code Online (Sandbox Code Playgroud)

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

请帮我解决一下这个.

Com*_*are 67

AFAIK,Android不支持OpenType.请改用TrueType字体.


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

  • 是.鉴于您的代码,您需要将您的字体放在assets /目录的根目录中,否则它应该与兼容字体完美匹配. (2认同)

Kir*_* A. 12

为了轻松访问我们的字体,我们需要将它与我们的应用程序捆绑在一起,以便我们的代码可以随后加载它.为此,我们直接在资产中创建一个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>
Run Code Online (Sandbox Code Playgroud)

在.java类中编写以下代码

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


ric*_*ett 9

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