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启动时将系统默认的字体替换为我们的自定义字体,偷梁换柱,悄悄的打枪,废话不多说,直接贴代码
然后在Application的OnCreate方法中调用FontsOverride.setDefaultFont(this,”MONOSPACE”,”fonts/xxx.ttf”);方法,将系统默认字体MONOSPACE替换为我们的自定义字体;
接下来就是将Application的theme设置为自定义Theme
实践发现Android5.0以后的机子用这个Theme不起作用,需要在res目录下新建values-v21文件夹并新建style.xml文件,重写theme
至此,应用中所有的字体都会被 替换成你想要的字体,当然用paint画出来的除外
最后感谢http://blog.csdn.net/xiaohui_hubei/article/details/49562005该贴的讲解
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轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- Kotlin简单实用方法既使用Kotlin优雅的开发Android应用
- android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- Kotlin简单实用方法既使用Kotlin优雅的开发Android应用
- Android应用开发实用方法小集
- Kotlin简单实用方法既使用Kotlin优雅的开发Android应用
- android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- android轻松管理安卓应用中的log日志 发布应用时log日志全部去掉的方法
- Android应用字体替换
- 让Python在Android系统上飞一会儿:第二节 第一个有实用价值的应用
- 【Android开发基础】应用界面主题Theme使用方法
- Android改变字体方法——Typeface
- android: 设置中文字体效果的一些方法
- 【Android开发基础】应用界面主题Theme使用方法
- Android应用开发揭秘里的EX_03_02获取联系人号码的获取方法的修正
- [Android]应用语言切换的三种方法
- Android改变字体方法——Typeface
- Android应用开发完全退出程序的通用方法!!!
- Android应用开发完全退出程序的通用方法!!! (转)
- eclipse 上调试android的自带应用方法 二 (Re-installation failed due to different application signatures.)