【发布时间】:2014-09-18 18:34:29
【问题描述】:
我正在开发一个 android 应用程序,但对如何更改 android 上的默认字体一无所知。我不想根系统来更改字体。必须在整个系统中更改字体。
【问题讨论】:
-
你可能正在搜索这个:stackoverflow.com/questions/2711858/…
我正在开发一个 android 应用程序,但对如何更改 android 上的默认字体一无所知。我不想根系统来更改字体。必须在整个系统中更改字体。
【问题讨论】:
setTypeface() 的唯一问题是,如果你想在应用范围内使用它,你必须在每个视图中都使用它。这要么意味着大量的冗余代码,要么意味着编写自定义视图类来设置字体膨胀。或者你可以在帮助类中使用这两个漂亮的小方法通过反射设置你的应用程序字体:
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 字体文件。
【讨论】:
你可以把你的自定义字体放在assets/fonts
它需要以 .ttf 扩展名结尾 而且比
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fonts/yourFont.ttf");
setTypeface(tf);
【讨论】:
创建资产/字体
示例代码:
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"));
【讨论】: