我们可以使用 BOLD / LIGHT 字体为整个应用程序实现字体覆盖吗?
我尝试了以下代码并更改了字体。但我无法实现大胆和轻松的字体样式。
td
然后在application onCreate方法中添加以下代码..
public class FontsOverride {
public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
replaceFont(staticTypefaceFieldName, regular);
}
protected static void replaceFont(String staticTypefaceFieldName,final Typeface newTypeface) {
if (Build.VERSION.SDK_INT >= 21) {
Map<String, Typeface> newMap = new HashMap<String, Typeface>();
String fontStyle = staticTypefaceFieldName.toLowerCase().replace("_", "-");
newMap.put(fontStyle, 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();
}
}
}
}
我尝试了
FontsOverride.setDefaultFont(this, "SANS_SERIF", "Montserrat-Regular.ttf");
但没有运气。我们有什么方法可以做到这一点吗?