【问题标题】:Using font in XML in API < 26在 API < 26 中使用 XML 中的字体
【发布时间】:2017-11-14 07:26:26
【问题描述】:

https://developer.android.com/preview/features/working-with-fonts.html

Android API 26 支持这一点,但我们是否有一个支持库可以提供使用字体作为资源中的资源? [ XML 中的字体 ] 如果是,那么它支持什么 API?

我说的是 Android O SDK 功能,它允许我们按照上面的链接在 XML 中设置字体。我敢肯定,这不是 MM 的原生功能。

【问题讨论】:

  • 我在我的一个应用程序中使用了这个功能,它支持 API 9 到 API 18。据我所知没有限制。我停止支持 API 8,但我想它也可以使用它。
  • 你在 gradle 中添加了哪个库?
  • 无。这是一个原生功能。顺便说一句,Eclipse 不需要所有那些“Gradle 东西”;)
  • 我想我被误解了。我说的是 O 中引入的“XML 中的字体”功能。编辑问题以澄清
  • with some little trick != 原生功能

标签: android fonts


【解决方案1】:

要支持API上低于26的字体系列,我们需要使用

<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>

而不是

<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"/>

【讨论】:

  • 谢谢。这应该被标记为正确答案。
【解决方案2】:

根据 Google 的说法,只要满足以下条件,Android 4.0+ (API 14+) 就支持它:

  • 您使用的是支持库版本26.0.0-beta1或更高版本
  • 您正在使用 AppCompat

来源:

Fonts in XML - Using the support library

Support Library revision history

我希望我可以在低于 8 (API 26) 的 Android 版本的应用小部件中使用它,但是这是不可能的,因为 AppCompatTextView 不能在应用小部件中使用。 Android O 的“XML 中的字体”的第三方替代品(例如 Caligraphy 库)也不能在应用小部件中使用。

应用小部件的唯一替代方法是使用 ImageView 而不是 TextView 并使用 RemoteViews 方法,例如 setImageViewUri(. ..)setImageViewBitmap(...),这两个都有问题。

【讨论】:

  • 谢谢。为什么其他人说它是原生功能?我正在使用 SDK Android Studio 2.3.2。我尝试使用@font/arial.ttf。它没有用。我使用的是支持库版本 25.3.1。现在我知道出了什么问题。
  • 这行得通吗?有人说你必须升级到 Android Studio 3 才能正常工作。
  • 感谢您的指点。我将 更改为 并且现在可以使用。
【解决方案3】:

为了完成 Yuri 的回答,字体支持回到 API 16。 要创建字体系列(指定不同的字体权重),您应该在 /res/font/ 下创建 my_font_family.xml,如 in the doc

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont_regular"/>
    <font app:fontStyle="normal" app:fontWeight="600" app:font="@font/myfont_semi_bold" />
    <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont_italic" />
</font-family>

要在TextView xml 中使用它,你应该使用app:fontFamily="@font/my_font_family",注意app: 而不是android:

要选择特定的fontWeight,您可以使用android:textFontWeight="200",但它仅适用于API>=28。要在 API 28 之前根据重量选择不同的字体,您有两种选择。您可以使用android:textStyle="bold" 仅指定“正常/斜体/粗体”,但不能指定确切的重量。或者您可以直接使用特定的字体重量,如下所示:app:fontFamily="@font/myfont_semi_bold"

请注意:如果您在应用中使用自定义文本视图YourTextView,它必须扩展AppCompatTextView 而不仅仅是android.widget.TextView

【讨论】:

  • 你试过textFontWeight吗?因为即使在 API 28 上它也不适合我。
  • 它对我不起作用,我在模拟器中运行。我正在使用实现 'androidx.appcompat:appcompat:1.0.2' 和 MaterialComponents 主题
  • @GaneshChowdharySadanala 请检查添加的注释,这可能会对您有所帮助。
  • textStyle 解决方案不适用于带有支持库的 Android 5
【解决方案4】:

一些用户会进入此页面,从 xml 中搜索在您的应用中使用自定义字体的说明。我是这样做的:

字体资源目录默认不存在。可以按照documentation here手动创建[工作量太大]

或者使用 GUI 中的字体选择器-

  • 在“设计”选项卡中选择一个 TextView。

  • 打开fontFamily xml 属性的下拉列表。

  • 向下滚动到“更多字体”。

  • 从出现的弹出窗口中选择“可下载字体”。

Android Studio 将使用上述文档中提到的必要声明自动创建字体资源目录。完成此步骤后,您可以将自定义字体文件(例如 myfont.ttf)复制到字体目录中,并从 xml 设置所需的字体,例如:

<TextView
     android:fontFamily="@font/myfont"
     android:id="@+id/aboutus_head_textview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/who_are_we" />

如果你想在你的应用中使用相同的字体,你可以在styles.xml中设置你的AppTheme的fontFamily:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- custom font for the entire app -->
        <item name="fontFamily">@font/myfont</item>
    </style>

[ 如果上述方法无效,则在 fontFamily 前面加上 android: ]

截图:

注意:此答案假定您使用的是 Android Studio 3+ 并支持库版本 26+

【讨论】:

    【解决方案5】:

    这里是一个例子,min sdk support 18 (in app)

    https://github.com/mukundrd/androidFonts

    【讨论】:

    • 是否可以包含较旧的 API?
    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多