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()); 

  • 相关阅读:
    最长k可重区间集问题【网络流24题】
    分配问题 【网络流24题】
    航空路线问题 【网络流24题】
    最长不下降子序列问题 【网络流24题】
    数组去重
    项目选题报告(团队)
    关于DLL的创建与使用简单描述(C++、C#)
    原型设计(结对第一次)
    团队展示(团队)
    第二次作业——个人项目实战
  • 原文地址:https://www.cnblogs.com/xunzhi/p/4496945.html
Copyright © 2011-2022 走看看