Android APP更换字体策略精要

作者: 肖丹晨 | 来源:发表于2017-10-20 14:11 被阅读176次

前言
近期项目需要在我们的APP中使用指定的字体库。经过搜集资料,研读源码,和别人探讨请教,最终产出了一些比较好的方案。不敢专享,写成文章分享出来,希望对大家的实际开发工作有所帮助。喜欢探讨Android开发技术的同学可以加学习小组QQ群: 193765960

本文只总结了较优方案,其他诸如自定义textView类,遍历layout_root_view这样的方案,作者认为限制较大,使用麻烦,就不在这里介绍了,感兴趣的朋友请自行百度。

版权归作者所有,如有转发,请注明文章出处:https://xiaodanchen.github.io/archives/

Android字体机制介绍

关键类:

  • Typeface:
    字体类,定义了字体类型到字体库的映射关系,Android有DEFAULT, MONOSPACE, SERIF, SANS_SERIF几种字体,根据各自的NORMAL(常规),BOLD(加粗),ITALIC(倾斜),BOLD_ITALIC(加粗倾斜)等几种样式,总共可以映射到至少16种字体库。
  • TextAppearance:
    字体外观类,定义了字体的外观比如,typeface,textsize,textcolor等外观属性。

TextView的字体显示机制

先看一下TextView的构造方法:

public TextView(Context context);
public TextView(Context context, @Nullable AttributeSet attrs);
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr);
public TextView(
            Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes);
  • AttributeSet:xml中设置的属性
  • defStyleAttr:系统默认的属性
  • defStyleRes:系统默认的样式,这个是我们需要注意的参数哈

Textview的字体设置逻辑:
1)查看xml中是否设置了TextAppearance属性,如果设置了就判断外观中是否设置了字体。否则就执行第二步。
2)查看xml中是否设置了Typeface属性,指明了字体。否则执行第三步
3)使用系统的默认样式:defStyleRes

所以,假如我们的xml中对字体没有做设置,要是想要修改字体又不想修改xml,那么我们就要想其他办法了。
我最终的方案(方案一)是在APP的theme中去设置修改系统的默认样式(最终走到这个思路上是经过了比较酸爽的经过的,就不在这里细说了)。

方案一(底层方案):通过反射机制,修改Typeface类的字体库引用

第一步:通过反射机制修改Typeface字体指向的字体库到我们的字体库。

  • 定义修改字体库的方法类(示例):
import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Typeface;
 
public final class FontsUtils {
 
  public static void setDefaultFont(Context context,
      String staticTypefaceFieldName, String fontAssetName) {
      final Typeface regular = Typeface.createFromAsset(context.getAssets(),
  fontAssetName);
      replaceFont(staticTypefaceFieldName, regular);
  }

  protected static void replaceFont(String staticTypefaceFieldName,
    final Typeface newTypeface) {
    try {
        final Field staticField = Typeface.class
        .getDeclaredField(staticTypefaceFieldName);
        staticField.setAccessible(true);
        staticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
  }
}
  • 在工程assets目录下新建fonts文件夹,把我们需要的字库放在里面,比如:FZLTHJW.TTF

  • 在MyApplication.oncreate()中调用修改字体库:

FontsUtils.setDefaultFont(this, "DEFAULT", "fonts/FZLTHJW.TTF");
FontsUtils.setDefaultFont(this, "MONOSPACE", "fonts/FZLTHJW.TTF");
FontsUtils.setDefaultFont(this, "SERIF", "fonts/FZLTHJW.TTF");
FontsUtils.setDefaultFont(this, "SANS_SERIF", "fonts/FZLTHJW.TTF");

第二步:修改APP theme的默认属性。

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textViewStyle">@style/FontTextviewstyle</item>
    <item name="android:buttonStyle">@style/FontButtonstyle</item>
    <item name="editTextStyle">@style/FontEditTextstyle</item>
    <item name="android:radioButtonStyle">@style/FontradioButtonstyle</item>
</style>
 
<style name="FontTextviewstyle" parent="android:style/Widget.TextView">
    <item name="android:textAppearance">@style/FontTextAppearance</item>
</style>
<style name="FontButtonstyle" parent="android:style/Widget.Button">
    <item name="android:textAppearance">@style/FontTextAppearance</item>
</style>
<style name="FontradioButtonstyle" parent="android:style/Widget.CompoundButton.RadioButton">
    <item name="android:textAppearance">@style/FontTextAppearance</item>
</style>
<style name="FontEditTextstyle" parent="Widget.AppCompat.EditText">
    <item name="android:textAppearance">@style/FontTextAppearance</item>
</style>
<style name="FontTextAppearance" parent="@android:style/TextAppearance">
        <item name="android:typeface">monospace</item>
</style>

总结:

  • 优点:
    • 不用修改xml,没有为每个activity创建字体的实例。
    • 除了常见的控件外,对Material Design的新控件也有作业
  • 缺陷:
    • 对于alertDialog还没有实现style的默认适配
    • 因为是修改的底层逻辑,相较于方案二,稍复杂。

方案二(顶层方案):自定义布局加载器,在加载layout_xml时对view tree的 view做字体的逻辑处理

  • 使用:如下方代码所示,在oncreatview的回调中,对view做类型判断,设置view的字体。
  • 优点:该方案代码逻辑清晰,使用简单,几行代码就可以搞定问题,不用修改xml等。
  • 缺陷:
    • 在一些第三方的控件或者自定义控件上可能使用会有限制,如果控件没有提供修改控件字体的接口的话(待验证)
    • 需要注意的是,对于Material Design的android.support.design.widget.TextInputLayoutandroid.support.design.widget.TabLayout这样的控件不起作用,需要对这种类型设置*textAppearance这样的属性。
    private void replaceFont() {
        final Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fangzheng.ttf");
        LayoutInflaterCompat.setFactory(LayoutInflater.from(this), new LayoutInflaterFactory() {
               
            @Override
            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
                // TODO Auto-generated method stub
                AppCompatDelegate delegate = getDelegate();
                View view = delegate.createView(parent, name, context, attrs);
                if(view != null ){
                    if(view instanceof TextView){
                        ((TextView)view).setTypeface(typeface);
                    }else if(view instanceof Button){
                        ((Button)view).setTypeface(typeface);
                    }else if(view instanceof RadioButton){
                        ((RadioButton)view).setTypeface(typeface);
                    }
                }
                return view;
            }
        });
    }
    
/**
*  BaseActivity.java
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    replaceFont();//注意需要在super方法之前调用,否则会报异常
    super.onCreate(savedInstanceState);
}

总结:

  • 通过这个方案,其实我们应该学习到一种统一对xml viewTree中某种控件设置某种属性的方法。
  • 举一反三,针对刚才上述的缺陷,我们其实也可以尝试设置textAppearance属性(相较于设置typeface麻烦些),感兴趣的同学可以去试验下。

相关文章

  • Android APP更换字体策略精要

    前言近期项目需要在我们的APP中使用指定的字体库。经过搜集资料,研读源码,和别人探讨请教,最终产出了一些比较好的方...

  • Android App更换字体调研

    最近各大项目都要换字体,所以调研了一下 首先,是换字体的方法:如果是TextView的话,一个是style中的fo...

  • android更换字体

    android更换字体 最近在研究android端字体替换需求,发现还是有很多方式来替换字体。 一,前言 Andr...

  • Android更换字体

    首先需要去找一些想使用的 ttf 文件, 去1001 Free Fonts或者Google Fonts找,或者让U...

  • Android更换字体

    Typeface typefaceLatoLight = Typeface.createFromAsset(con...

  • Flutter 更换App图标

    以Flutter2.0+版本为示例 iOS App图标更换 Android App图标更换

  • LayoutInflaterFactory app内全局更换字体

    LayoutInflaterFactory app内全局更换字体 LayoutInflater 大家肯定很熟悉,布...

  • Android开发--更换字体

    开发中,经常遇到需要更换字体格式,设计妹子为了让UI更美,设置了很多让人着(tong)迷(ku)的字体,但是蓝瘦归...

  • Android Studio--项目实战中的坑

    Android Studio 版本3.4.2Android 版本最高9.0 更换APP的应用图标 从android...

  • iOS-动态下载所需的字体

    1、更换系统字体的方法 添加字体文件 使用苹果官方API动态下载动态下载的优点:1)不占用APP包的体积(字体文件...

网友评论

    本文标题:Android APP更换字体策略精要

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