android - Unresolved reference : font in Android Studio

标签 android android-studio kotlin fonts

我正在尝试在 android studio 中为 TextView 设置字体,但由于某些原因,我无法访问字体目录。

我得到Unresolved Reference: font

我做了什么

- 通过右键点击 res > new > Android Resource Directory > Directory name : font > Resource type : font 创建了一个新目录

- 将我的字体放在该目录中,因此该目录读取 - res/font/myfont.ttf

- 在我的 fragment 中,我尝试像这样访问字体

var typeFace: Typeface? = ResourcesCompat.getFont(requireContext(), R.font.myfont.ttf)

错误显示Unresolved Reference: font

我尝试重建项目,但仍然无法正常工作。

最佳答案

使用:

typeFace: Typeface? = ResourcesCompat.getFont(requireContext(), R.font.myfont)

不要添加.ttf扩展名。

关于android - Unresolved reference : font in Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64114911/

相关文章:

android - AndroidManifest.xml 中的属性 application@allowBackup value=(false) 也出现在 [ :barcodescanner:] AndroidManifest. xml value=(true)

android - 将复选框添加到单选集 AlertDialog

java - 使用其他类(class)的 NFC Activity

java - 尝试在空对象引用上调用虚拟方法 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)'

android - 无法查询属性 'applicationId' 的值,因为项目 ':presentation' 的配置尚未完成

kotlin - 如何使用 RxJava2 将 api 中的数据组合到域对象中?

android - 我想在没有用户交互的情况下自动打印

android - 在 Android 中,有没有办法使用 "MODE_IN_COMMUNICATION"将音频路由到 A2DP?

android - Kotlin 代码堆栈跟踪显示 Java 行号

android - 无法使用 Mockito 测试使用协程和挂起函数的代码