zoukankan      html  css  js  c++  java
  • android 更换字体

    1、 必须事先在assets底下创建一fonts文件夹 并放入要使用的字体文件(.ttf)

    2、写一个manager

    /**
     * 设置字体
     * 
     */
    public class FontManager {
    
        static String fongUrl = "fonts/myfonttff.ttf";
    
        static Typeface tf;
    
        public static void applyFont(Context context, ViewGroup root, String fontName) {
    
            Typeface tf = getFont(context);
    
            for (int i = 0; i < root.getChildCount(); i++) {
                View v = root.getChildAt(i);
                if (v instanceof TextView) {
                    ((TextView) v).setTypeface(tf);
                } else if (v instanceof Button) {
                    ((Button) v).setTypeface(tf);
                } else if (v instanceof EditText) {
                    ((EditText) v).setTypeface(tf);
                } else if (v instanceof ViewGroup) {
                    applyFont(context, (ViewGroup) v, fontName);
                }
            }
        }
    
        public static Typeface getFont(Context context) {
    //        if (tf == null) {
    //            tf = Typeface.createFromAsset(context.getAssets(), fongUrl);
    //        }
    //        return tf;
            return Typeface.DEFAULT;
        }
    }
    

      

    3、重写TextView

    /**
     * 自定义TextView
     * 
     */
    public class MyTextView extends TextView {
    
        public MyTextView(Context context) {
            super(context);
            init(context);
        }
    
        public MyTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
        
        public MyTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }
    
        private void init(Context context) {
             Typeface setFont = FontManager.getFont(context);
             if (setFont != null) {
                 setTypeface(setFont);
             }
        }
    
    }
    

      

    4、在xml中直接用MyTextView,呆着全路径,直接替换TextView 

      另一种textView.setTypeface(FontManager.getFont()); 

  • 相关阅读:
    $("").append无反应
    go 客户端、服务端
    go mysql insert变量到数据库
    .gvfs: Permission denied
    go笔记
    java socket通信笔记
    (转)linux中top命令下显示出的PRNIRESSHRS\%MEM TIME+都代表什么
    adb Android Debug Bridge 安卓调试桥
    一阶段冲刺(四)
    一阶段冲刺(三)
  • 原文地址:https://www.cnblogs.com/xunzhi/p/4496945.html
Copyright © 2011-2022 走看看