我正在创建一个要在其中设置自定义字体的应用程序。

但是我不能在.xml文件中使用自定义字体,因为要使用此字体,我需要初始化.java文件中的每个TextView。

此过程非常冗长且耗时。

如果有人知道,请帮助我...

最佳答案

供你引用,

 public class MyTextView extends TextView {

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

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

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

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
        setTypeface(tf ,1);

    }
}

在XML中,
 <you_package.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Your text"
        />

10-07 12:59
查看更多