android - 在 androidx 中使用自定义字体

标签 android fonts androidx

我目前正在尝试向我的 Android 项目添加自定义字体系列,该项目使用 Androidx 而不是旧的支持库。

但我似乎无法找到一种方法来使用 AndroidX 库支持旧 SDK 版本(23+)中的字体系列,我在网上找到的所有内容都提到了旧的支持库。

我当前的字体系列文件如下所示:

<font-family
             xmlns:app="http://schemas.android.com/apk/res-auto">

    <font
        app:fontStyle="normal"
        app:fontWeight="500"
        app:font="@font/raleway_medium"/>

    <font
        app:fontStyle="normal"
        app:fontWeight="700"
        app:font="@font/raleway_bold" />

    <font
        app:fontStyle="normal"
        app:fontWeight="600"
        app:font="@font/raleway_semibold" />

</font-family>

有没有办法通过 androidx 库在旧版 SDK 上运行?

最佳答案

您必须使用 appandroid 属性声明您的字体。所以它适用于“所有”sdk 版本。

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

当你声明你的字体时,你可以通过以下方式在 xml 中应用它:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:fontFamily">@font/customfont</item>
    <item name="fontFamily">@font/customfont</item>
...

或以编程方式通过:

val typeface = ResourcesCompat.getFont(context, R.font.customfont)
textView.typeface = typeface

关于android - 在 androidx 中使用自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233080/

相关文章:

java - 使用 MediaCodec 进行压缩

Android:提高应用搜索结果排名

android - 开发报警应用

css - Sitecore Google 字体在生产环境中不工作

flutter - Flutter APK构建Android资源链接失败的振动包

android - ActivityManager 不再需要我的应用

Android react-native 应用程序不拾取字体

python-2.7 - 使用 PIL 绘制多语言文本并保存为 1 位和 8 位位图

Androidx迁移: Program type already present androidx.并发.futures.DirectExecutor

java - 解决 com.android.support :support-compat and androidx. core:core 之间的依赖冲突