添加自定义字体到Android应用程序

时间:2013-11-20 11:49:59

标签: java android android-fonts

我正在尝试将自定义字体添加到我的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>

3 个答案:

答案 0 :(得分:2)

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

取代:

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

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

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

  txtvw.setTypeface(typface);
相关问题