5

カスタムフォントをTextViewに設定する方法を知っています。しかし、theme / style/xmlを介してカスタムフォントを追加する方法は??

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="customfontforlistview" parent="@android:style/Widget.ListView">
        <item name="android:textColor">#000000</item>
        <item name="android:typeface">HOW_TO_CUSTOM_FONT</item>
   </style>
</resources>

このための解決策を提供してください。

4

1 に答える 1

3

私の知る限り、これを行う良い方法はありません。私が知っている最良の代替手段は、次のような単純なサブクラスを作成することです。

public class CustomTextViewNormal extends TextView
{
    public CustomTextViewNormal(Context context)
    {
        super(context);
        init();
    }

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

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

    public void init()
    {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fonts/HelveticaNeue.ttf");
        setTypeface(tf);
    }
}

これを作成すると、このカスタム テキスト ビューを xml レイアウトで使用するだけで、基本的にはスタイルと同じ機能を使用できますが、確かに少しずさんです。

于 2012-04-16T18:50:55.717 に答える