所有
我使用follow代码更改字体,但在某些Android设备中不支持FragmentActivity。 谁能帮助我,而不是
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();
}
}
}
然后您需要重载几个默认字体,例如:
FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset3.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>
答案 0 :(得分:0)
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">@style/monospaceTextViewStyle</item>
<item name="android:buttonStyle">@style/monospaceButtonStyle</item>
</style>
<style name="monospaceTextViewStyle" parent="android:Widget.TextView">
<item name="android:typeface">monospace</item>
</style>
<style name="monospaceButtonStyle" parent="android:Widget.Holo.Button">
<item name="android:typeface">monospace</item>
</style>
注意:这适用于API版本&gt;只有16 ..
答案 1 :(得分:0)
我已获得其他解决方案,请参阅以下源代码
将您的字体文件复制到assets文件夹。 资产 --fonts --FuturaLT.ttf --XXXX.ttf --FuturaLT.ttf
在您的应用类中添加源代码
公共类MyApp扩展了Application {
public MyApp() {
super();
}
@Override
public void onCreate() {
super.onCreate();
// change App Font
changeAppFont();
}
private void changeAppFont(){
setDefaultFont(this, "DEFAULT", "fonts/FuturaLT.ttf");
setDefaultFont(this, "DEFAULT_BOLD", "fonts/FuturaLT-Bold.ttf");
setDefaultFont(this, "MONOSPACE", "fonts/FuturaLT-Oblique.ttf");
setDefaultFont(this, "SANS_SERIF", "fonts/FuturaLT.ttf");
setDefaultFont(this, "SERIF", "fonts/FuturaLT.ttf");
}
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();
}
}