android 改变全局字体样式
参考:http://stackoverflow.com/questions/2711858/is-it-possible-to-set-font-for-entire-application/16883281#16883281
1.将要替换的字体包放到assets下
2.具体代码:
//设置新的字体样式
public static void setDefaultFont(Context context, StringstaticTypefaceFieldName, String fontAssetName) {
final Typeface regular =Typeface.createFromAsset(context.getAssets(),fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
//代替原来的文本样式
protected static void replaceFont(String staticTypefaceFieldName, finalTypeface newTypeface) {
Log.i("yyy",staticTypefaceFieldName+","+newTypeface.getStyle());
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();
}
}
3.在style.xml中设置,在清单文件中引入使用
serif
android:name="com.example.androidstudy.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >...
4.在application中使用:
FontUtils.setDefaultFont(this, "SERIF","fonts/ff.ttf");
5.最终效果