Android 自定义字体

1. Android默认方法

为每个view单独设置Typeface.

Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/AMJT.ttf");((TextView) findViewById(R.id.tv_hello)).setTypeface(customFont);

这种方法可以扩展一下,将常用的Typeface保存下来,避免频繁创建销毁对象造成开销过大引起卡顿。

1.1 新建一个工具类

public class Typefaces {    private static final Map mCache = new HashMap<>();    public static Typeface get(Context context, String name) {        synchronized (mCache) {            if (!mCache.containsKey(name)) {                Typeface t = Typeface.createFromAsset(context.getAssets(), "fonts/" + name + ".ttf");                mCache.put(name, t);            }            return mCache.get(name);        }    }}

1.2 然后在需要的地方调用get函数

TextView view = (TextView) findViewById(R.id.tv_hello);view.setTypeface(Typefaces.get(this, "AMJT"));

2. 扩展TextView,Button等类

复写其setTypeface方法

或者 通过自定义属性来实现

3. 通过ViewGroup来递归设置字体

3.1 新建一个helper类

public class TypefaceHelper {    public static void setTypeface(final Context context, final View view, String fontName) {        if (view instanceof ViewGroup) {            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {                View v = ((ViewGroup) view).getChildAt(i);                setTypeface(context, v, fontName);            }        } else if (view instanceof TextView) {            ((TextView) view).setTypeface(Typefaces.get(context, fontName));        }    }}

这里复用了上面的Typefaces类

3.2 在activity中做如下设置(onCreate中)

TypefaceHelper.setTypeface(this, findViewById(android.R.id.content), "AMJT");

这种方式可以参考一个开源项目,自己实现的更完善些。https://github.com/norbsoft/android-typeface-helper

我们可以实现一个基类,在onCreate中调用这个方法来设置所有子view的typeface

4 全局设置

全局设置貌似并没有什么特别好的方法。

我之前的项目中使用了一种侵入性很强的方法。

思路是:利用反射在程序中替换掉系统默认的字体。

4.1 在application中定义如下方法

private void setDefaultFont(Context context, String fontAssetName) {  final Typeface regular = Typeface.createFromAsset(context.getAssets(),                                                    fontAssetName);  replaceFont(regular);}private void replaceFont(final Typeface newTypeface) {  Map newMap = new HashMap<>();  Typeface typeface = Typeface.create("sans-serif", Typeface.NORMAL);  /* 设置默认字体为方正兰亭,roboto-regular原来的key是sans-serif,现在设置为roboto-regular         * xml或者java中如果要是用roboto-regular请使用"roboto-regular"这个key         * XML example:         *     android:fontFamily="roboto-regular"         * Java example:         *     Typeface typeface = Typeface.create("roboto-regular", Typeface.NORMAL);         * FIXME: 目前发现Button的字体改变不了,需要follow上面的代码手动设置,XML或者JAVA         */  newMap.put("sans-serif", newTypeface);  newMap.put("roboto-regular", typeface);  try {    final Field staticField = Typeface.class      .getDeclaredField("sSystemFontMap");    staticField.setAccessible(true);    staticField.set(null, newMap);  } catch (NoSuchFieldException | IllegalAccessException e) {    e.printStackTrace();  }}

然后在Application的onCreate中调用:

setDefaultFont(this, "fonts/FZLTHK.TTF");

之前以为这种方式会简单,效率高点,会更方便,但是实现起来却又很大的问题。

  1. 侵入性太强,利用反射修改了系统的默认字体(不会影响到其他应用)。
  2. 对Button不起作用,没有找到答案。对于Button还得手动去设置,并没有减少太多工作量,还是有很多的重复代码

5 结论

综上,还是方法4相对更简单每次activity create的时候会重新设置所有子view的typeface,对于全局替换的实现还是很方便的。

对于方法4,实现的比较好的一种方式可以参考https://github.com/norbsoft/android-typeface-helper。不过不建议为了字体而特意引入一个库,可以直接实现一个简单的,也更轻量级的,满足自己项目需求就行了。

更多相关文章

  1. android 几个工具方法
  2. Android中共享全局变量
  3. Android 判断当前设备是手机还是平板的最有效的方法
  4. Android创建Alert框的方法
  5. Android studio 学习1:实现点击事件的4种方法
  6. 解决方法:android 6.0(api 23) SDK,不再提供org.apache.http.*(只
  7. Android中SQLite数据库操作(2)——使用SQLiteDatabase提供的方法操
  8. 【Android笔记】Activity涉及界面全屏的方法
  9. Android Studio查看源码时出现Sources for ‘Android API 30 Pla

随机推荐

  1. android矢量图vector的简单介绍
  2. Android实现动态改变屏幕方向(Landscape &
  3. Android Studio系列教程三--快捷键
  4. Android之音量调节
  5. Android应用程序——四大组件之Activity
  6. 前端和Android / IOS 对接问题
  7. Android坐标系、视图坐标系与触控事件(Mot
  8. Android技术栈
  9. [Android] [Java] 分享 Process 执行命令
  10. Android 连接tomcat模拟登陆账号