Android:如何将自定义字体添加到theme/style/xml

Dil*_*rla 5 fonts android

我知道如何将自定义字体设置为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>
Run Code Online (Sandbox Code Playgroud)

请为此提供解决方案.

Sal*_*kan 4

据我所知,没有一个好的方法可以做到这一点。据我所知,最好的选择是创建一个简单的子类,如下所示:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你做到了这一点,你就可以在 xml 布局中使用这个自定义文本视图,并且你基本上具有与样式相同的功能,尽管不可否认,有点马虎。