【问题标题】:How to set a downloadable font for a whole application as default font?如何将整个应用程序的可下载字体设置为默认字体?
【发布时间】:2018-05-21 20:20:56
【问题描述】:

在我之前的项目中,我使用Calligraphy 库为整个应用程序设置字体。但它需要将字体文件存储在资产中,这使得 APK 尺寸更大。现在我想知道是否可以将downloadable font 设置为整个应用程序的默认值。

我只能为一个 TextView 设置可下载的字体。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:fontFamily="@font/lato"
    android:text="@string/large_text" />

是的,我知道我可以创建MyTextView 类并以编程方式设置可下载字体。但我认为这不是一个好主意,因为文本可以在 EditText、Spinner 项、Toast 中的任何位置。

所以我的问题是如何将整个应用程序的可下载字体设置为默认字体?

【问题讨论】:

    标签: android android-fonts android-downloadable-fonts


    【解决方案1】:

    要在您的应用中随处应用 XML 字体集,请在您的 themes.xml 中创建一个主题并在其中设置 android:fontFamily

    <style name="ApplicationTheme">
        <item name="android:fontFamily">@font/lato</item>
    </style>
    

    在 Manifest 中将此主题设置为您的应用程序

    <application
        android:name=".App"
        android:icon="@mipmap/ic_launcher"
        android:theme="@style/ApplicationTheme">
    ...
    </application>
    

    只要您不使用继承自系统样式的样式,例如

    <style name="CustomButton" parent="Base.Widget.AppCompat.Button.Borderless">
    

    您的字体将应用于所有 TextView、EditText、Spinner、Toast 等。

    【讨论】:

      【解决方案2】:

      A library to help with custom fonts and text sizes

      此库的目标是让您的应用以一种易于配置的方式支持具有自己的样式(例如,普通、粗体、斜体)的多个 FontFamilies(例如 lato、roboto 等)。

      【讨论】:

      • 但是这个库不适用于整个应用程序
      【解决方案3】:
      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();
              }
          }
      }
      
      
      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");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-01
        • 2021-10-07
        • 2014-02-09
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 2013-04-30
        • 2018-03-24
        相关资源
        最近更新 更多