您的位置:首页 > 移动开发 > Android开发

Android 替换应用内字体轻松又实用的方法

2016-09-04 17:58 351 查看
转载自:http://www.eoeandroid.com/thread-921819-1-1.html?_dsign=4a488641

1、自定义TextView;

优点:使用简单方便,不需要额外的工作。

缺点:只能替换一类控件的字体,如果需要替换Button或EditText控件的字体,需要以相同的方式自定义这些控件,这样工作量大。

2、递归替换根布局下所有的View字体;

优点:不需要修改XML布局文件,不需要重写控件,可以批量替换所有继承自TextView的控件的字体,适合需要批量替换字体的场合,如程序的默认字体。

缺点:如果要替换整个App的所有字体,需要在每个有界面的地方批量替换一次,页面多了还是有些工作量的,不过可以在Activity和Fragment的基类中完成这个工作。其次,性能可能差一点,毕竟要递归遍历所有子节点(不过实际使用中没有明显的性能下降程序依然流畅)。

3、通过反射替换默认字体

这种方法的实现思路就是在APP启动时将系统默认的字体替换为我们的自定义字体,偷梁换柱,悄悄的打枪,废话不多说,直接贴代码

public class FontsOverride {

public static void setDefaultFont(Context context,
String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(),
fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}

public static boolean isVersionGreaterOrEqualToLollipop() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return true;
}
return false;
}

protected static void replaceFont(String staticTypefaceFieldName,final Typeface newTypeface) {
if (isVersionGreaterOrEqualToLollipop()) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
newMap.put("sans-serif", newTypeface);
try {
final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
staticField.setAccessible(true);
staticField.set(null, newMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else {
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();
}
}
}
}


然后在Application的OnCreate方法中调用FontsOverride.setDefaultFont(this,”MONOSPACE”,”fonts/xxx.ttf”);方法,将系统默认字体MONOSPACE替换为我们的自定义字体;

接下来就是将Application的theme设置为自定义Theme

<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:typeface">monospace</item>
</style>


实践发现Android5.0以后的机子用这个Theme不起作用,需要在res目录下新建values-v21文件夹并新建style.xml文件,重写theme

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:textAppearance">@style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:typeface">monospace</item>
</style>


至此,应用中所有的字体都会被 替换成你想要的字体,当然用paint画出来的除外

最后感谢http://blog.csdn.net/xiaohui_hubei/article/details/49562005该贴的讲解
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android