1

ttfがassetsxmlを介して自分のフォルダにあるフォントを設定するにはどうすればよいですか?私はそれをプログラムで行う方法を知っていますが、xmlを介してそれをどのように行うことができますか?前もって感謝します。

4

5 に答える 5

14

XML を直接使用してこれを行うことはできませんがTextView、デフォルトのフォントを拡張して設定することはできます。

package com.nannu;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NanTV extends TextView{

    private Context c;
    public NanTV(Context c) {
        super(c);
        this.c = c;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }
    public NanTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);
        // TODO Auto-generated constructor stub
    }

    public NanTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }


}

そして、あなたのレイアウトでは new を使用しますTextView

<com.nannu.NanTV
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 

        />

前の回答からこれを投稿していますhttps://stackoverflow.com/a/11239305/1166537

于 2012-07-13T13:47:20.060 に答える
3

独自の派生を作成する場合はTextView、クラスがプログラムによって処理する XML 属性を追加できます。このように、特定のフォントを指定すると実行時に設定されますが、XML 経由で行います。:)

それ以外の場合、要求された動作を実現する既知の方法はありません。

于 2012-07-13T13:43:17.043 に答える
1

これを行うには、カスタム TextView を記述します。それ以外の場合は、プログラムで設定する必要があります。詳細については、このリンクを参照してください

于 2012-07-13T13:50:32.557 に答える
1

ここでこの問題を解決するためのライブラリを作成しました: http://responsiveandroid.com/2012/03/15/custom-fonts-in-android-widgets.html

TextView を拡張し、xml で書体の名前を設定します。ダウンロード可能なサンプルがあります。

于 2014-12-11T17:52:25.773 に答える