【问题标题】:Using a custom font for all texts in my app为我的应用程序中的所有文本使用自定义字体
【发布时间】:2018-07-01 14:49:55
【问题描述】:

procedure 之后,我想将 android 应用程序中所有文本的字体设置为特定名称。这是一种非英文字体,所以我将其粘贴到app\src\main\assets\fonts 并在styles.xml 中写入以下内容

    <item name="android:fontFamily">@fonts/my.ttf</item>
    <item name="fontFamily">@fonts/my.ttf</item>

但是,在 android studio 中构建并运行应用程序后(模拟器弹出),我看到的是默认字体,而不是我指定的字体。

顺便说一句,在那篇文章中,它使用@font。但是,如果我这样写,我会看到找不到字体的错误。通过更改@fonts,编译错误解决!

这里缺少什么?

【问题讨论】:

    标签: android custom-font


    【解决方案1】:

    创建字体系列

    字体系列是一组字体文件及其样式和粗细细节。在 Android 中,您可以创建一个新的字体系列作为 XML 资源并将其作为一个单元进行访问,而不是将每个样式和粗细作为单独的资源引用。通过这样做,系统可以根据您尝试使用的文本样式选择正确的字体。

    要创建字体系列,请在 Android Studio 中执行以下步骤:

    1. 右键单击字体文件夹并转到新建 > 字体资源文件。将出现“新建资源文件”窗口。
    2. 输入文件名,然后单击确定。新字体资源 XML 将在编辑器中打开。
    3. 将每个字体文件、样式和粗细属性包含在元素中。以下 XML 说明了在字体资源 XML 中添加字体相关属性:

        **<font-family 
              <font
                  android:fontStyle="normal"
                  android:fontWeight="400"
                  android:font="@font/lobster_regular" />
              <font
                  android:fontStyle="italic"
                  android:fontWeight="400"
                  android:font="@font/lobster_italic" />
          </font-family>**
      

    向 TextView 添加字体

    要为 TextView 设置字体,请执行以下操作之一:

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lobster"/>
    

    Check this out 获取完整文档。

    希望对你有所帮助。

    【讨论】:

      【解决方案2】:

      如果你想覆盖所有视图的字体,你可以使用这种方法:

      public static Typeface 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);
              return customFontTypeface;
          } catch (NoSuchFieldException e) {
              Logger.e(e);
              return null;
          } catch (IllegalAccessException e) {
              Logger.e(e);
              return null;
          }
      }
      

      然后在 Application 类的 onCreate() 方法中调用这个方法:

      TypefaceUtil.overrideFont(this,
                  "SERIF",
                  "fonts/CaviarDreams.ttf");
      

      不要忘记将您的 .ttf 字体文件放入 assets 文件夹中

      【讨论】:

      • 使用 Android Studio 3.1,您无需复制 assets 目录下的任何 ttf 文件。它直接支持在 res 目录下创建包含你的 ttf 文件的字体目录
      • 现在我得到cannot find symbol class Typeface
      • 我在super.onCreate()之前打过电话
      • @mahmood add this import import android.graphics.Typeface;
      • 现在我得到error: cannot find symbol TypefaceUtil
      猜你喜欢
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 2013-10-22
      • 2014-10-10
      • 2012-03-21
      相关资源
      最近更新 更多