Android 替换应用内字体轻松又实用的方法

转载自:http://www.eoeandroid.com/thread-921819-1-1.html?_dsign=4a488641


1、自定义TextView;
优点:使用简单方便,不需要额外的工作。
缺点:只能替换一类控件的字体,如果需要替换Button或EditText控件的字体,需要以相同的方式自定义这些控件,这样工作量大。
2、递归替换根布局下所有的View字体;
优点:不需要修改XML布局文件,不需要重写控件,可以批量替换所有继承自TextView的控件的字体,适合需要批量替换字体的场合,如程序的默认字体。
缺点:如果要替换整个App的所有字体,需要在每个有界面的地方批量替换一次,页面多了还是有些工作量的,不过可以在Activity和Fragment的基类中完成这个工作。其次,性能可能差一点,毕竟要递归遍历所有子节点(不过实际使用中没有明显的性能下降程序依然流畅)。
3、通过反射替换默认字体
这种方法的实现思路就是在APP启动时将系统默认的字体替换为我们的自定义字体,偷梁换柱,悄悄的打枪,废话不多说,直接贴代码

    public class FontsOverride {


        public static void setDefaultFont(Context context,
                                          String staticTypefaceFieldName, String fontAssetName) {
            final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                    fontAssetName);
            replaceFont(staticTypefaceFieldName, regular);
        }

        public static boolean isVersionGreaterOrEqualToLollipop() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                return true;
            }
            return false;
        }

        protected static void replaceFont(String staticTypefaceFieldName,final Typeface newTypeface) {
            if (isVersionGreaterOrEqualToLollipop()) {
                Map<String, Typeface> newMap = new HashMap<String, Typeface>();
                newMap.put("sans-serif", newTypeface);
                try {
                    final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
                    staticField.setAccessible(true);
                    staticField.set(null, newMap);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
                    staticField.setAccessible(true);
                    staticField.set(null, newTypeface);
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

然后在Application的OnCreate方法中调用FontsOverride.setDefaultFont(this,”MONOSPACE”,”fonts/xxx.ttf”);方法,将系统默认字体MONOSPACE替换为我们的自定义字体;
接下来就是将Application的theme设置为自定义Theme

    <style name="AppBaseTheme" parent="android:Theme.Light">
        </style>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="AppBaseTheme">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:typeface">monospace</item>
        </style>

实践发现Android5.0以后的机子用这个Theme不起作用,需要在res目录下新建values-v21文件夹并新建style.xml文件,重写theme

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        </style>

        <!-- Application theme. -->
        <style name="AppTheme" parent="AppBaseTheme">
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:windowNoTitle">true</item>
            <item name="android:textAppearance">@style/CustomTextAppearance</item>
        </style>
    <style name="CustomTextAppearance">
            <item name="android:typeface">monospace</item>
        </style>

至此,应用中所有的字体都会被 替换成你想要的字体,当然用paint画出来的除外
最后感谢http://blog.csdn.net/xiaohui_hubei/article/details/49562005该贴的讲解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值