android - 在 API 26 之前从 TypedArray 获取字体资源

标签 android

Android O 通过支持库将 xml 中的字体引入回 API 16。但是我找不到与 TypedArray.getFont() 等效的支持,它需要 API 级别 26。

val array = context.obtainStyledAttributes(styleResId, R.styleable.TextAppearance)
val font = array.getFont(R.styleable.TextAppearance_android_fontFamily) // nope

是否有某种兼容实用程序类可用于从样式资源 ID 检索字体?

最佳答案

找到了一个解决方法,就是从TypedArray中找到字体的资源ID,然后使用ResourcesCompat加载字体。

val array = context.obtainStyledAttributes(styleResId, R.styleable.TextAppearance)
if (array.hasValue(R.styleable.TextAppearance_android_fontFamily)) {
    val fontId = array.getResourceId(R.styleable.TextAppearance_android_fontFamily, -1)
    val typeface = ResourcesCompat.getFont(context, fontId)
}

关于android - 在 API 26 之前从 TypedArray 获取字体资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45339115/

相关文章:

android - 如何将 android.graphics.Path 作为字符串或 Json 发送到另一部手机?

android - facebook 在 android 应用程序中的集成

java - Android View /fragment 问题

android - 我们如何在 Android Jetpack Compose UI 中将文本格式化为上标或下标?

android - 在 Kindle 设备中创建推送通知时出错?

android - 如何通过 React Native 将打印作业发送到打印机

android - 如何使用 Facebook 3.0 SDK 处理 Android 应用程序中的 API 错误?

android - 无法使用 android phonegap facebook-connect , twitter 插件

android toast打印奇怪的数字而不是单词

android - 删除 Flutter 中两列之间的空间