效果圖對比.png
工具類
/************************************************************
* Author: ssc
* Date: 2018/1/11
* Version: 1.0
* Description:字體替換工具類
* History:
* <author> <time> <version > <desc>
***********************************************************/
public class FontsUtils {
/**
* 設置自定義字體
*
* @param context
* @param staticTypefaceFieldName 需要替換的系統(tǒng)字體樣式
* @param fontAssetName 替換后的字體樣式
*/
public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
// 根據(jù)路徑得到Typeface
Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
// 設置全局字體樣式
replaceFont(staticTypefaceFieldName, regular);
}
private static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
try {
final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
staticField.setAccessible(true);
//替換系統(tǒng)字體樣式
staticField.set(null, newTypeface);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
工具類使用
步驟一和二.png
步驟三.png
步驟四.png
FontsApplication中代碼
public class FontsApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//設置全局默認字體樣式
FontsUtils.setDefaultFont(this, "SERIF", "fonts/FZLTXHJW.TTF");
}
}