0

私は自分のアプリケーションアンドロイドに新しいフォントを適用しようとしていて、style.xmlから呼び出すことができます

例えば。私のフォントは assets/thaoma.ttf にあります

style.xml

<style name="text_black_color_bold" >
        <item name="android:textColor">#3E3E3E</item>
        <item name="android:textStyle">bold</item>
        <item name="android:typeface"></item> /* call new font here */
    </style>
4

2 に答える 2

1

xmlから含めることはできません。あなたはそのようなコードを通してそれをしなければなりません:

final TextView myTextView = getTextView();
final AssetManager assets = ctx.getAssets();
final Typeface font = Typeface.createFromAsset(assets, "thamoa.ttf");
setTypeface(font);

気の利いたトリックの1つは、TextViewを拡張し、実行時にフォントを自動適用することです。

public class CustomTextView extends TextView {

    public CustomTextView(Context ctx) {
        super(ctx);
        setupText(ctx);
    }

    public CustomTextView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        setupText(ctx);
    }

    public CustomTextView(Context ctx, AttributeSet attrs, int defStyle) {
        super(ctx, attrs, defStyle);
        setupText(ctx);
    }

    private void setupText(Context ctx) {
        // check if in edit mode and return. Fonts can't be applied when viewing from editor
        if(isInEditMode()) {
           return;
        }

        final AssetManager assets = ctx.getAssets();
        final TypeFace font = Typeface.createFromAsset(assets, "thamoa.ttf");
        setTypeface(font);
    }
}

次に、同じように使用できますが、xmlでそのように参照します。

<package.containing.class.CustomTextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   <!-- whatever attributes that would normally apply here -->
 />
于 2013-01-24T19:13:05.867 に答える