将自定义字体添加到

将自定义字体添加到

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

最佳答案



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


代替:

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

07-24 09:21