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

  • 相关阅读:
    ajax 请求登录超时跳转登录页的示例代码
    [WPF]实现密码框的密码绑定
    Linq系列(5)——表达式树之案例应用
    idea设置内存大小
    idea右下角显示使用内存情况
    idea打开Run Dashboard
    java的byte[]与String相互转换
    java有包名的调用没有包名的类,用反射
    【转】查看电脑显卡型号及显卡性能
    idea关闭sonarLint自动扫描
  • 原文地址:https://www.cnblogs.com/xunzhi/p/4496945.html
Copyright © 2011-2022 走看看