简体   繁体   English

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

[英]android no resource found font family @font/your_font_xml

I am trying to use Font as a Resource in XML instead of using Typeface.我正在尝试将字体用作 XML 中的资源,而不是使用字体。

I have kept .ttf files in /res/font directory .我将.ttf文件保存在/res/font directory中。

I have implemented something like this我已经实现了这样的事情

In Layout XMLLayout XML

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

In res/font/font.ttf在 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>

But still I am getting the error as below.但我仍然收到如下错误。

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

I am referring this and this docs to implement this behavior.我指的是这个这个文档来实现这个行为。

Thanks.谢谢。

Upgrade your gradle version to 4.1 将Gradle版本升级到4.1

Do this by editing gradle-wrapper.properties 通过编辑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 file 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>

make sure this is correct and it should present in font folder and rebuild the project (invalidate caches/restart) 确保这是正确的,并且应该出现在字体文件夹中并重建项目(使缓存无效/重新启动)

Try renaming your font into the ideal file name format, which is:尝试将您的字体重命名为理想的文件名格式,即:

All font names must be only: lowercase az, 0-9, or underscore.所有字体名称只能是:小写 az、0-9 或下划线。

You can change font location from 您可以从更改字体位置

res/font directory

to

\assets\fonts

It worked for me, i hope it can help your problem 它对我有用,我希望它可以帮助您解决问题

You are making font folder inside resource .It is wrong . 您正在资源内部制作字体文件夹。这是错误的。

You have to make font folder inside asset 您必须在素材资源中创建字体文件夹

Follow this Steps. 请遵循以下步骤。

Right Click on app > New > Folder > Assets Folder

查看此图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM