通过反射替换默认字体

App中显示的字体来自于Typeface中的预定义的字体,这种方式是通过改变系统字体样式改变字体。

首先需要改变APP的BaseTheme

<!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><!-- Set system default typeface --><item name="android:typeface">monospace</item></style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

再然后我将需要的方法又抽取了一下,和之前的TypefaceUtil形成了一个完整的工具类,代码如下:

package com.laundrylang.laundrylangpda.util;import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import java.lang.reflect.Field;/** Copyright (C) 2013 Peng fei Pan <sky@xiaopan.me>** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**   http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
public class TypefaceUtil {/*** 为给定的字符串添加HTML红色标记,当使用Html.fromHtml()方式显示到TextView 的时候其将是红色的** @param string 给定的字符串* @return*/public static String addHtmlRedFlag(String string) {return "<font color=\"red\">" + string + "</font>";}/*** 将给定的字符串中所有给定的关键字标红** @param sourceString 给定的字符串* @param keyword      给定的关键字* @return 返回的是带Html标签的字符串,在使用时要通过Html.fromHtml()转换为Spanned对象再传递给TextView对象*/public static String keywordMadeRed(String sourceString, String keyword) {String result = "";if (sourceString != null && !"".equals(sourceString.trim())) {if (keyword != null && !"".equals(keyword.trim())) {result = sourceString.replaceAll(keyword, "<font color=\"red\">" + keyword + "</font>");} else {result = sourceString;}}return result;}/*** <p>Replace the font of specified view and it's children</p>* @param root The root view.* @param fontPath font file path relative to 'assets' directory.*/public static void replaceFont(@NonNull View root, String fontPath) {if (root == null || TextUtils.isEmpty(fontPath)) {return;}if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's fontTextView textView = (TextView)root;int style = Typeface.NORMAL;if (textView.getTypeface() != null) {style = textView.getTypeface().getStyle();}textView.setTypeface(createTypeface(root.getContext(), fontPath), style);} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child viewsViewGroup viewGroup = (ViewGroup) root;for (int i = 0; i < viewGroup.getChildCount(); ++i) {replaceFont(viewGroup.getChildAt(i), fontPath);}}}/*** <p>Replace the font of specified view and it's children</p>* 通过递归批量替换某个View及其子View的字体改变Activity内部控件的字体(TextView,Button,EditText,CheckBox,RadioButton等)* @param context The view corresponding to the activity.* @param fontPath font file path relative to 'assets' directory.*/public static void replaceFont(@NonNull Activity context, String fontPath) {replaceFont(getRootView(context),fontPath);}/** Create a Typeface instance with your font file*/public static Typeface createTypeface(Context context, String fontPath) {return Typeface.createFromAsset(context.getAssets(), fontPath);}/*** 从Activity 获取 rootView 根节点* @param context* @return 当前activity布局的根节点*/public static View getRootView(Activity context){return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);}/*** 通过改变App的系统字体替换App内部所有控件的字体(TextView,Button,EditText,CheckBox,RadioButton等)* @param context* @param fontPath* 需要修改style样式为monospace:*/
//    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
//    <!-- Customize your theme here. -->
//    <!-- Set system default typeface -->
//    <item name="android:typeface">monospace</item>
//    </style>public static void replaceSystemDefaultFont(@NonNull Context context, @NonNull String fontPath) {replaceTypefaceField("MONOSPACE", createTypeface(context, fontPath));}/*** <p>Replace field in class Typeface with reflection.</p>*/private static void replaceTypefaceField(String fieldName, Object value) {try {Field defaultField = Typeface.class.getDeclaredField(fieldName);defaultField.setAccessible(true);defaultField.set(null, value);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142

代码中引用只需要在BaseApplication的onCreate里设置,为了增强效果,这里我将显示样式变成粗体:

TypefaceUtil.replaceSystemDefaultFont(this,"fonts/bold.ttf");
  • 1

全局替换安卓应用字体相关推荐

  1. 全局替换字体,开源库更方便!!!

    序 在 Android 下使用自定义字体已经是一个比较常见的需求了,最近也做了个比较深入的研究. 那么按照惯例我又要出个一篇有关 Android 修改字体相关的文章,但是写下来发现内容还挺多的,所以我 ...

  2. android手机可以换字体吗,安卓怎么换字体 安卓手机字体替换教程

    傻瓜式安卓字体更换应用:字体精灵软件大小:2.6M 软件性质:免费 支持系统:安卓(Android) 下载方法:字体精灵 v2.1 百度搜索或者在各大安卓手机应用市场搜索,均可找到下载安装 字体精灵是 ...

  3. Windows系统全局替换字体总结

    Win+R,输入regedit,按HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Fonts路径打开,右侧双击Micros ...

  4. 全局替换字体的 4 种方式

    前言 很高兴遇见你~ 在本系列的上一篇文章中,我们了解了 Xml 中的字体,还没有看过上一篇文章的朋友,建议先去阅读 Android字体系列 (三):Xml中的字体,有了前面的基础,接下来我们就看下 ...

  5. Windows11全局修改系统默认字体

    全局替换Windows默认的微软雅黑字体,我们需要想办法把另一种字体伪装成微软雅黑. 这里我自己使用的是荣耀字体,已伪装好了下载解压即可 荣耀字体下载链接https://cowtransfer.com ...

  6. eclipse 全局替换

    2019独角兽企业重金招聘Python工程师标准>>> 快捷键Ctrl+H 1.选中要替换内容 2.全局替换 快捷键Ctrl+H 3.替换内容 点击ok就可以了. 转载于:https ...

  7. 华为安卓转鸿蒙,坦白说,华为不用鸿蒙替换安卓,而用HMS替代GMS,是当前最好方案 - 区块网...

    众所周知,最近华为的HMS很受网友们的关注,因为在华为手机海外版无法安装谷歌GMS的情况之下,华为用自已的HMS替代了谷歌的GMS. 其实说起来,如果你在国内购买华为手机的,HMS已经早就使用了,因为 ...

  8. vim的全局替换[zz]把字符替换成回车

    本文出自   http://blog.csdn.net/shuangde800 本文是在学习<使用vi编辑器, Lamb & Robbins编著>时在所记的笔记. 本文内容: 基本 ...

  9. 学习vi和vim编辑器(8):全局替换(1)

    本章学习vi编辑器中的全局替换命令.通过全局替换命令,可以自动替换文件中所有出现过的某个单词.全局替换一般会用到两个ex命令:":g"(global),":s" ...

最新文章

  1. Android数据适配器Adapter简介
  2. 自然语言处理之jieba分词
  3. 数字预失真技术基本原理
  4. Linux基础(5)--Linux常用命令表
  5. 中小型公司***的配置及NAT应用案例
  6. 第二次项目冲刺(Beta阶段)--第五天
  7. RFID扫描APP Android
  8. 31位圈内大佬解读DApp困惑:“爆款”也难优秀!
  9. JSjavascript获取B站封面图片超高清批量下载原图
  10. Java基础入门及安装准备
  11. SUN ZFS STORAGE 7320阵列管理
  12. 初中计算机卡片的制作教案,其他教案-贺卡的设计与制作
  13. H7-TOOL脱机烧录器支持1拖4,支持新唐,GD32,MM32,AT32,APM32,CX32,STM32,STM8,i.MX RT,W7500,外置Flash等2020-10-27
  14. 求真值表,主析取范式,主合取范式
  15. 华硕 ROG主题 提取主题包
  16. 微信小程序商城有什么功能?
  17. MySQL数据库卸载+MySQL常用的图形化管理工具介绍
  18. filebeat7.7.0相关详细配置预览- processors - rename
  19. 安卓开发笔记-UI设计的概念
  20. 音频文件如何转换成MP3格式?一分钟教你搞定

热门文章

  1. 利用T-SQL处理SQL Server数据库表中的重复行
  2. sql 时态表的意义_SQL Server中的时态表
  3. Thread 1: signal SIGABRT
  4. 数据类型的基本表达式
  5. 最小公倍数最大公约数
  6. LINUX mysql 源码安装
  7. asp.net AJAX 使用webServices调用时,出现“WebService”未定义
  8. 转载 lemontrees(lemontree) 的计算机系考研攻略 (游戏版)
  9. 华为综合测评是什么_喝水不用等待,温度随心控随时喝到热水,测评华为智选恒温电水壶...
  10. 装饰模式-包装request和response