我正在开发一个Android应用程序,并且对如何更改android上的默认字体一无所知。我不想根系统以更改字体。必须在整个系统中更改字体。
答案 0 :(得分:1)
setTypeface()的唯一问题是,如果你想要app wide,你必须在每个视图中使用它。这或者意味着大量的冗余代码,或者编写自定义视图类来设置通货膨胀的字体。或者您可以在帮助器类中使用这两个漂亮的小方法来通过反射设置应用程序字体:
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) {
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();
}
}
然后设置字体执行类似MyHelperClass.setDefaultFont(this, "DEFAULT", "fonts/FONT_NAME.ttf");
这会将您应用的默认字体设置为FONT_NAME.ttf字体文件。
答案 1 :(得分:0)
您可以将自定义字体放在assets/fonts
它需要以.rtf扩展名结尾 然后
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/yourFont.ttf");
setTypeface(tf);
答案 2 :(得分:0)
创建资产/字体
示例代码:
number_text = (TextView)findViewById(R.id.hour_progress_number);
number_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
minute_text = (TextView)findViewById(R.id.minute_progress_number);
minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));