Android自定义字体

作者: Terryho | 来源:发表于2017-10-09 16:01 被阅读15次

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<String, Typeface> 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<String, Typeface> 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。不过不建议为了字体而特意引入一个库,可以直接实现一个简单的,也更轻量级的,满足自己项目需求就行了。

相关文章

  • android自定义字体

    Android O通过字体资源支持自定义字体,支持.otf(OpenType)和.ttf(TrueType)字体格...

  • Android自定义字体

    文章目的:快速入门Android中自定义各种字体! 前言:我们都知道,Android中默认的字体是黑体,而大多数a...

  • Android自定义字体设置

    Android自定义字体设置 1.typeface、fontFamily、textStyle介绍 1.typefa...

  • Android 自定义字体

    说明 在一些应用中,有改变字体,或者某些页面有特殊字体要求时,Android自身的字体满足不了时,需要自定义字体,...

  • Android自定义字体

    Android 自定义字体 1. Android默认方法 为每个view单独设置Typeface. 这种方法可以扩...

  • Android上的自定义字体 - 通过XML进行动态字体选择

    前言 这是我们系列自定义字体在Android上的第2篇文章。在之前,我们已经看到如何使用自定义字体并将不同的样式应...

  • Android 自定义字体方案

    在应用中需要配置不同的字体,而 Android 只能在 xml 中配置系统默认提供的四种字体,需要自定义的字体都需...

  • Android APP支持自定义字体

    情景:需要为整个应用替换自定义字体。 Android对于文字的字体设置主要是通过以下两个对象 FontFamily...

  • Android 设置字体

    吐槽 首先,来点吐槽。在Android O 之前设置字体是如此之…… 缺点:自定义字体只能通过动态代码设置; 每次...

  • 十九、Expo 使用自定义字体

    使用自定义字体 iOS和Android都有自己的平台字体集,但如果你想在你的应用中注入更多的品牌个性,那么选择好的...

网友评论

    本文标题:Android自定义字体

    本文链接:https://www.haomeiwen.com/subject/medpyxtx.html