字体下载网址:自定义字体.7z
1 复制工具类

package com.lym.uifont.utils;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;/*** @author liyanmei* @date 2019/6/25* @describe*/
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();}}
}

2.修改style样式

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --><!-- Set system default typeface --><item name="android:typeface">monospace</item></style>

3.将需要用到的ttf文件导入src/main/assets/fonts文件夹下
4.application的onCreate方法中调用

package com.lym.uifont;import android.app.Application;import com.lym.uifont.utils.TypefaceUtil;/*** @author liyanmei* @email lym_liyanmei@qq.com* @date 2019/6/25* @describe*/
public class MyApp extends Application {@Overridepublic void onCreate() {super.onCreate();TypefaceUtil.replaceSystemDefaultFont(this,"fonts/font_zhangcao.ttf");}
}

5 androidmanifest配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.lym.uifont"><applicationandroid:name=".MyApp"android:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

参考网址:Android APP修改全局字体

Android APP修改全局字体相关推荐

  1. 太激动!Android修改全局字体样式,替换整个APP字体

    最近一直在如何全局修改app字体上困惑着,今天终于有了突破.我将搜集的资料进行了整理,现在提供给大家. 前面为分析,建议直接翻到最后看[个人中心设置]. 参考链接: Android应用使用自定义字体 ...

  2. Android全局修改字体大小,Android 仿微信全局字体大小调整

    image 目录 一.前言 二.效果预览 三.实现步骤 1.自定义字体调整控件 2.滑动按钮改变当前页面预览字体大小 3.返回时,保存放大倍数并重启应用 4.初始化应用时配置字体放大倍数. 四.Dem ...

  3. Android App修改字体大小,且不随系统字体大小更改

    在做混合开发时发现,无论是APP内的字体大小,还是前端的字体大小,都会随着系统字体大小发生变化.当遇到老人字体(特大号字体)时,有些页面的布局就乱掉了.而玩过游戏的都知道,所有游戏APP的字体都不会随 ...

  4. android 怎么获取app 字体颜色,Android APP使用自定义字体实现方法

    android系统内置字体 android 系统本身内置了一些字体,可以在程序中使用,并且支持在xml配置textView的时候进行修改字体的样式.支持字段为android:textStyle ,an ...

  5. android 自定义字体 ttf,Android APP支持自定义字体

    情景:需要为整个应用替换自定义字体. Android对于文字的字体设置主要是通过以下两个对象 FontFamily.Typeface 在XML文件中设置单个字体: android:id="@ ...

  6. 将win10修改全局字体为苹方字体

    文章目录 字体分享 使用软件 注册表修改 声明 获取字体名称 效果展示 恢复原来的字体 字体分享 百度云链接 提取码:ioh2 若是失效的化可以私信我. 使用软件 noMeiryoUI 使用这个软件, ...

  7. android webview 设置文字颜色,android webView 修改页面字体颜色

    webview加载一个界面后,在onPageFinished中加入修改页面字体颜色css,如下String nightCode = ""; try { InputStream is ...

  8. Flutter Android app 修改启动背景颜色和logo——筑梦之路

    打开android\app\src\main\res\drawable\launch_background.xml, <?xml version="1.0" encoding ...

  9. android 怎么获取app 字体颜色,android app 修改字体

    android中可能会遇到修改字体的情况,虽然说需求比较少,但是偶尔还会遇到 可以使用三方框架来帮助我们简单做到 api "uk.co.chrisjenx:calligraphy:2.2.0 ...

最新文章

  1. 一周AI看点 | 董明珠投资150亿洛阳造机器人 北京首条无人驾驶地铁线空载试运行
  2. ZooKeeper基础学习
  3. 32位 shell.efi x86_通过grub,让32位的efi也能运行64位的Linux发行版
  4. AutoLayout框架之序言
  5. 下pg负载均衡_SAE 场景下,应用流量的负载均衡及路由策略配置实践
  6. 在一个html中使用另一个html数据,如何为某些HTML标签存储任意数据
  7. leetcode942. DI String Match
  8. 引用: 编写高性能 Web 应用程序的10个技巧
  9. Spring AOP 浅析
  10. VC编程-预编译头文件(precompiled header)
  11. 提取excel表数据成json格式的以及对图片重命名
  12. DBSCAN 聚类算法详解
  13. cnn kaggle仙人掌_我如何开发可识别情绪并闯入Kaggle前10名的CNN
  14. 学习MVC之租房网站(二)-框架搭建及准备工作
  15. Codeforces Round #744 (Div. 3) B. Shifting Sort
  16. 引领企业电销革新,外呼系统是不可缺的电销工具
  17. 编译时出现stripped of unavailable superclass
  18. hp服务器怎么安装xp系统,windowsxp系统安装惠普打印机软件的方法
  19. 现代信号处理——平稳随机信号通过线性系统
  20. 【EXPDP】11g版本EXPDP 的COMPRESSION参数压缩比堪比“gzip -9”

热门文章

  1. QQ声音商标官司折射特定声音注册之难
  2. 二进制安装多master节点的k8s集群
  3. Red Giant Trapcode Suite 12.0 含注册码
  4. 小学四年级家长计算机,小学四年级家长的寄语
  5. 《Head First Java》| 2-3 拜访对象村+认识变量
  6. gdal支持的栅格影像格式说明
  7. Zabbix-低级自动发现
  8. UNIX系统常用命令
  9. 【报告分享】产业互联网发展报告-亿邦智库(附下载)
  10. POJ 1364 King