我需要为整个应用程序使用某些字体。我有同样的.ttf文件。
是否可以在应用程序启动时将此字体设置为默认字体,然后在应用程序中的其他位置使用它?设置后,如何在布局XML中使用它?
最佳答案
是的,反射(reflection)。这有效(based on this answer):
(注意:由于缺乏对自定义字体的支持,因此这是一种解决方法,因此,如果要更改此情况,请加注星标以投票android issue here)。 注意:不要在该问题上留下“我也”评论,每当您注视它时,都会收到一封电子邮件。所以,请给它“加星标”。
import java.lang.reflect.Field;
import android.content.Context;
import android.graphics.Typeface;
public final 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) {
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();
}
}
}
然后,您需要重载一些默认字体,例如在application类中:
public final class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");
}
}
或者当然,如果您使用的是相同的字体文件,则可以对此进行改进以仅将其加载一次。
但是,我倾向于只覆盖一个,比如
"MONOSPACE"
,然后设置一种样式来强制该字体应用程序扩展到整个应用程序:<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:typeface">monospace</item>
</style>
</resources>
API 21 Android 5.0
我已经对评论中的报告进行了调查,认为该报告无效,并且似乎与主题
android:Theme.Material.Light
不兼容。如果该主题对您不重要,请使用较旧的主题,例如:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:typeface">monospace</item>
</style>