我想在我的应用程序中使用外部字体。我尝试使用AssetManager
添加新的fonts
,但没有成功。以下是我的代码:
Typeface face;
face = Typeface.createFromAsset(getAssets(), "font.otf");
textview.setTypeface(face);
但是它没有显示文本...
请帮助我解决这个问题。
我想在我的应用程序中使用外部字体。我尝试使用AssetManager
添加新的fonts
,但没有成功。以下是我的代码:
Typeface face;
face = Typeface.createFromAsset(getAssets(), "font.otf");
textview.setTypeface(face);
但是它没有显示文本...
请帮助我解决这个问题。
据我所知,Android不支持OpenType字体。请使用TrueType字体代替。
更新: 显然现在已经支持OpenType,至少在某种程度上。起初它是不支持的,因此您需要在您的应用程序将支持的任何Android版本上全面测试您的字体。
<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手机上问题会因为像素更大而被极大放大。
安卓支持otf和ttf两种字体格式,我都有使用过。
tv3 = (TextView)findViewById(R.id.tv1);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/TRAJANPRO-BOLD.OTF");
tv3.setTypeface(typeFace);
这是我用于英语和本地语言的步骤
我曾经遇到过同样的问题。我的字体在安卓上也无法使用,但我需要它能够工作。使用字体编辑器,我将我的字体中的字符复制到了 Android-src-2_1 中 FontSampler 示例自带的字体中。这样就完美解决了。
虽然我承认从知识产权的角度来看,我的方法有些可疑,但实际上我并没有使用原始字体,因为所有字符都被替换了,所有对旧字体的引用也被替换了。我曾尝试“查看”两种字体的定义方式,但是让所有字体变量匹配也不起作用。所以最终,我使用原始字体的框架作为新字体的模板。
Typeface face; face = Typeface.createFromAsset(getAssets(), "fonts/" +"font.otf"); textview.setTypeface(face);
- Gogu CelMare