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

  • 相关阅读:
    每天一个css text-indent
    每天一个css word-break word-wrap white-space
    [转载]CentOS+nginx+uwsgi+Python+django 环境搭建
    【转】django使用model创建数据库表使用的字段
    Django对mysql操作
    mysql增删改查
    mysql用户管理
    centos7启动mysql
    centos安装python3
    [转载]python学习目录(转至袁先生的博客)
  • 原文地址:https://www.cnblogs.com/xunzhi/p/4496945.html
Copyright © 2011-2022 走看看