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 に答える