如何为android中的所有视图使用相同的自定义字体?

Moh*_*wan 5 android view android-custom-view

我想使用android中具有相同字体类型的所有组件,因为我正在为CustomTextView,CustomEditText等每个组件创建一个单独的自定义类.

因此,我可以创建一个视图CustomView类,它将自动为android中的所有组件应用样式,而不是为每个组件创建单独的类

A.S*_*.S. 1

只需声明您自己的 TextView 并在 XML 中使用它,它应该出现在自定义视图中

 public class MyTextView extends TextView {

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

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

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

private void setType(Context context){
    this.setTypeface(Typeface.createFromAsset(context.getAssets(), "chalk_board.ttf"));
}
Run Code Online (Sandbox Code Playgroud)

噢,你希望它在全球范围内适用于所有视图,所以这是错误的方法......对此感到抱歉