【问题标题】:how to use a same custom fontface for all the view in android?如何为android中的所有视图使用相同的自定义字体?
【发布时间】:2013-10-08 12:49:52
【问题描述】:

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

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

【问题讨论】:

    标签: android view android-custom-view


    【解决方案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"));
    }
    

    哦,老妈,你希望它在全局范围内用于所有视图,所以这是错误的方法.... 抱歉

    【讨论】:

    • 感谢您的信息,但我已经创建了您建议我的相同内容,但问题是所有视图的字体必须相同,因此我应该创建一个扩展组件的类所有类型的视图。所以我需要一个类,它会影响android中所有视图的字体,就像默认样式一样......
    【解决方案2】:

    你至少有两种方法:

    • 创建自己的 TextView 类和set fontFace in constructor
    • 您可以使用自定义 LayoutInflater。每次视图膨胀时,检查它是否是 textView(或其他不扩展 textView 但具有字体设置的视图)并设置正确的 fontFace 设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 2013-02-23
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多