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/