2

我正在尝试将自定义字体添加到我的 android 应用程序。这是我的代码。谁能告诉我我错在哪里?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textview = (TextView) findViewById(R.id.FontTest);

        Typeface tf = Typeface.createFromAsset(getApplicationContext()
                .getAssets(), "fonts/snap-itc.ttf");
        textview.setTypeface(tf);
    }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView 
        android:id="@+id/FontTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font Test"
        android:textSize="50px"/>
</RelativeLayout>
4

3 回答 3

2

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/snap-itc.ttf");

代替:

Typeface tf = Typeface.createFromAsset(getApplicationContext()
                .getAssets(), "fonts/snap-itc.ttf");
于 2013-11-20T11:52:15.307 回答
0

这可能很明显,但请确保您的项目目录中有一个/assets/fonts文件夹,并且您的自定义字体在那里。此外,检查字体的正确拼写(+ 是大写还是小写)并更正代码中的拼写。

这也可能意味着字体本身已损坏。

于 2013-11-20T13:07:25.997 回答
0

在您的应用程序中添加字体,例如

 TextView txtvw=(TextView)findViewById(R.id.textviewname);

 Typeface typface=Typeface.createFromAsset(getAssets(),"fonts/burmese.ttf");

  txtvw.setTypeface(typface);
于 2015-02-28T19:07:36.853 回答