android没有资源找到字体系列@ font / your_font_xml

时间:2017-10-31 10:14:19

标签: android fonts android-resources

我正在尝试使用Font作为XML中的资源而不是使用 字体。

我在.ttf中保留了/res/font directory个文件。

我已经实现了类似的东西

Layout XML

<Button android:fontFamily="@font/my_font"/>

在res / font / font.ttf

<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
    android:fontStyle="normal"
    android:fontWeight="40"
    android:font="@font/my_font" />
</font-family>

但我仍然得到如下错误。

Error:(156, 33) No resource found that matches the given name (at 'fontFamily' with value '@font/font').`

我指的是thisthis个文档来实现此行为。

感谢。

3 个答案:

答案 0 :(得分:0)

将您的gradle版本升级到4.1

通过编辑 gradle-wrapper.properties

来完成此操作
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

xml文件

 <?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">
 <font
    android:fontStyle="normal"
    app:font="@font/rupee" //this line for below android 26 devices
    android:font="@font/rupee" />

  </font-family>

确保这是正确的,它应该出现在font文件夹中并重建项目(使缓存/重启无效)

答案 1 :(得分:-1)

您可以从

更改字体位置
res/font directory

\assets\fonts

它对我有用,我希望它可以帮助你解决问题

答案 2 :(得分:-1)

您正在资源中创建字体文件夹。这是错误的。

您必须在资产

中设置字体文件夹

按照以下步骤操作。

Right Click on app > New > Folder > Assets Folder

Check This Image