【问题标题】:Prevent custom system fonts overriding app fonts防止自定义系统字体覆盖应用程序字体
【发布时间】:2019-10-03 03:34:35
【问题描述】:

Samsung 等设备允许用户设置custom system fonts。问题是这些字体可能会覆盖我的应用程序fonts。我的意思是,如果我在我的应用程序中设置Arial font 并在我的手机上将Calibri 字体设置为system font,那么Arial 将被Calibri font 覆盖。

如何预防这种情况?

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:
    • "如果您需要为您的Android 中的所有TextView's 设置一种字体 应用程序,您可以使用此解决方案。
    • 它将覆盖 ALL TextView's 字体,包括Action Bar,自定义系统 fonts 等标准组件,但EditText's密码字体 不会被覆盖。”(原因很明显)。

    • 使用reflection 覆盖默认字体和Application 类。

    • 注意:不要忘记将应用主题的字体设置为将被覆盖的默认字体

    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:

    <?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:

    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:

    把 your 字体放在这里,例如Arial.

    ref1

    作者Artem Zinnatullin

    【讨论】:

    • 据我了解解决方案,我必须首先识别我想用我的字体覆盖的字体。我的应用程序将商业化,我无法识别用户将使用哪种字体作为自定义系统字体。
    • 没有。您可以使用font_title = Typeface.DEFAULT; 获取默认的Typeface 您还可以根据指定的样式获取默认的字体:Typeface.defaultFromStyle(int style) 另见how-to-install-a-system-font-programmatically-on-a-samsung-android-device
    • 好的,我会再试一次,然后告诉你
    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2022-10-14
    • 2020-07-27
    • 1970-01-01
    • 2013-11-01
    • 2021-11-01
    • 2023-02-14
    • 1970-01-01
    相关资源
    最近更新 更多