0

XML でアプリの Roboto フォントを変更する必要があり、RunTime で Roboto フォントを変更したいと考えています。それ、どうやったら出来るの?

4

1 に答える 1

3

これにはアセット ファイルを使用します。ファイルにフォントをダンプしassetsます。

Typefaceそれに応じてクラスセットのフォントスタイルを使用します。

Typeface style = Typeface.createFromAsset(asm, "fonts/Roboto-Bold.ttf");
view.setTypeFace(style);

ここで、Roboto フォントはアセット内の別のディレクトリ fonts 内に保存されていることに注意してください。

より良い実践のために、必要なビューを拡張する別のクラスを作成できます。パッケージ名とともに、作成されたビューに xml レイアウトでアクセスできます。

    public class BoldTextView extends TextView{

    public BoldTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    public BoldTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public BoldTextView(Context context) {
        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/Roboto-Bold.ttf");
        setTypeface(tf);
    }

}

レイアウトでのこのクラスへの参照:

com.your.packagename.BoldTextView
于 2013-12-27T08:31:04.037 に答える