字体下载网址:自定义字体.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 "" + string + "";    }    /**     * 将给定的字符串中所有给定的关键字标红     *     * @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, "" + keyword + "");            } else {                result = sourceString;            }        }        return result;    }    /**     * 

Replace the font of specified view and it's children

* * @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 font TextView 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 views ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); ++i) { replaceFont(viewGroup.getChildAt(i), fontPath); } } } /** *

Replace the font of specified view and it's children

* 通过递归批量替换某个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: */// public static void replaceSystemDefaultFont(@NonNull Context context, @NonNull String fontPath) { replaceTypefaceField("MONOSPACE", createTypeface(context, fontPath)); } /** *

Replace field in class Typeface with reflection.

*/ 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样式

  

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 {    @Override    public void onCreate() {        super.onCreate();        TypefaceUtil.replaceSystemDefaultFont(this,"fonts/font_zhangcao.ttf");    }}

5 androidmanifest配置

<?xml version="1.0" encoding="utf-8"?>                                                                                        

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

更多相关文章

  1. Android(安卓)mk编译OkHttp3
  2. Android中对于Geometry对象的字符串化
  3. Android下修改SeekBar样式
  4. android 常用方法集合
  5. Android: 修改ramdisk.img
  6. android之listView之增删改查
  7. Ubuntu 10.04编译Android(安卓)2.2 源代码
  8. android lint检查出现的问题
  9. Android(安卓)Handler Message 传递字符串

随机推荐

  1. TypedArray和obtainStyledAttributes使用
  2. Android(安卓)7.0拍照后保存到手机的指定
  3. android之Can't create handler inside t
  4. android下的读写权限
  5. android studio之NDK配置
  6. android.os.FileUriExposedException 自
  7. Android(安卓)撸起袖子,自己封装 DialogFr
  8. 七 APPIUM Android(安卓)定位方式
  9. Android(安卓)studio运行Main方法
  10. 使用Android(安卓)Studio创建OpenCV 4.1.