android全局字体更换

作者 mexican 日期 2017-06-28
android全局字体更换

全局更换字体

-

通过反射获取主题字体并设置新字体
private void initTypeface(){
try {
Field field = Typeface.class.getDeclaredField("SERIF");
field.setAccessible(true);
field.set(null, Typeface.createFromAsset(getAssets(), "fonts/msyh.ttf"));
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
```
```java
在AppTheme中添加设置主题字体
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- 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:typeface">serif</item>
</style>