[英]Prevent custom system fonts overriding app fonts
Devices like Samsung
allows users to set custom system fonts
. Samsung
等设备允许用户设置custom system fonts
。 Issue is that those fonts can be overriding my app fonts
.问题是那些 fonts 可以覆盖我的应用程序
fonts
。 I mean if I set Arial
font
in my app and I've set Calibri
font as system font
on my mobile then Arial
will be overridden by the Calibri font
.我的意思是,如果我在我的应用程序中设置了
Arial
font
,并且我在我的手机上将Calibri
字体设置为system font
,那么Arial
将被Calibri font
覆盖。
How to prevent this situation?如何预防这种情况?
- "If you need to set one font for all
TextView's
in yourAndroid
application you can use this solution.“如果您需要为
Android
应用程序中的所有TextView's
设置一种字体,您可以使用此解决方案。It will override ALL
TextView's
typefaces, including theAction Bar
, custom system fonts and other standard components, butEditText's
password font won't be overridden." (For obvious reasons).它将覆盖所有
TextView's
字体,包括Action Bar
、自定义系统 fonts和其他标准组件,但不会覆盖EditText's
密码字体。”(原因很明显)。Using
reflection
to override default typeface and theApplication
class.使用
reflection
覆盖默认字体和Application
class。- NOTE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
注意:不要忘记将应用程序主题的字体设置为将被覆盖的默认字体
MyApp.java : MyApp.java :
public class MyApp extends Application {
@Override
public void onCreate() {
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf
}
}
res/themes.xml : res/themes.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyAppTheme" parent="@android:style/Theme.Holo.Light">
<!-- you should set typeface which you want to override with TypefaceUtil -->
<item name="android:typeface">serif</item>
</style>
</resources>
TypefaceUtil.java :字体Util.java :
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.lang.reflect.Field;
public class TypefaceUtil {
/**
* Using reflection to override default typeface
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
* @param context to work with assets
* @param defaultFontNameToOverride for example "monospace"
* @param customFontFileNameInAssets file name of the font from assets
*/
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
}
}
}
assets/fonts/Roboto-Regular.ttf:资产/字体/Roboto-Regular.ttf:
Put your font here eg Arial
.将您的字体放在这里,例如
Arial
。
Author Artem Zinnatullin作者Artem Zinnatullin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.