简体   繁体   English

使用Android O的“android:fontFamily”时出现错误“文件名必须以.xml结尾”?

[英]Getting error “file name must end with .xml” while using the “android:fontFamily” of Android O?

Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources. Android O 引入了一项新功能,即 XML 中的字体,可让您将字体用作资源。 And I am creating the font folder in resource file as it provided by Android developer , but the problem is that I am getting error that file name must end with .xml while using the font folder which is provided in Android O release.我正在Android 开发人员提供的资源文件中创建font文件夹,但问题是我在使用 Android O 版本中提供的font文件夹时遇到file name must end with .xml错误。

Please check the below layout for it.请检查下面的布局。

 <TextView
   android:id="@+id/txtMsgCount"
   android:layout_width="wrap_content"
   android:layout_height="16dp"
   android:background="@drawable/msg_count"
   android:gravity="center"
   android:text="123"
   android:fontFamily="@font/Montserrat_Regular" ////IT IS MY FONT STYLE
   android:textColor="@android:color/white"
   android:textSize="10sp" />

And please check the font folder which I have created in res with Montserrat_Regular.otf file并请检查我在res使用Montserrat_Regular.otf文件创建的font文件夹

我的字体文件夹与字体样式

I am getting the following error while using the above, which are as follows:使用上述方法时出现以下错误,具体如下:

获取错误

I have searched about it but did not get expected result, please check:我已经搜索过但没有得到预期的结果,请检查:

1. First Link 1. 第一个链接
2. Second Link 2. 第二个链接
3. Third Link 3. 第三个链接
4. Forth Link 4.第四链接

And the Font resource file is not creating as I am clicking the Right-click the font folder and go to New > Font resource file .并且Font resource file没有创建,因为我正在单击Right-click the font folder and go to New > Font resource file But not getting the option of the Font resource file , please check below:但是没有获得Font resource file的选项,请检查以下内容:

在此处输入图片说明

Font resources are not fully supported by the Android Studio 2.3.3 build chain. Android Studio 2.3.3 构建链不完全支持字体资源。 Full support comes with Android Studio 3.0+ and its associated Android Plugin for Gradle version.完全支持 Android Studio 3.0+ 及其相关的 Android Plugin for Gradle 版本。 At that point, you will be able to create a font/ resource directory that goes alongside layout/ , menu/ , etc.那时,您将能够创建一个font/资源目录,与layout/menu/等并列。

IOW, hold off on this until you are ready to upgrade to Android Studio 3.0. IOW,在您准备好升级到 Android Studio 3.0 之前,暂缓此操作。

I've lost an hour on this error, using AS 3.0 beta2, Gradle 4.x and the correct build-tools.我在这个错误上浪费了一个小时,使用 AS 3.0 beta2、Gradle 4.x 和正确的构建工具。

Turned out I had a copy of the fonts in a folder called "fonts".原来我在一个名为“fonts”的文件夹中有一份字体的副本。 This folder doesn't show up in the Android project view.此文件夹不会显示在 Android 项目视图中。 And reading the error I didn't catch the 's'.阅读错误我没有抓住's'。 Just a small reminder for you readers!只是给各位读者一个小小的提醒!

I just had the same problem and while searching for an answer, I came here on this thread.我刚刚遇到了同样的问题,在寻找答案时,我来到了这个线程。 I've found a solution from deverloper.android.com .我从deverloper.android.com找到了一个解决方案。

  1. Right click 'res' folder in Android Studio右键单击 Android Studio 中的“res”文件夹

  2. Choose 'new' > 'Android resource directory'选择“新建”>“Android 资源目录”

  3. Name it 'font'将其命名为“字体”

  4. Inhere you should place your font (.ttf) files WITH ONLY az 0-9 and _ characters.您应该在这里放置仅包含az 0-9 和 _ 字符的字体 (.ttf) 文件。

  5. Set your font (mine is called 'alegreyaregular.ttf') this way:以这种方式设置您的字体(我的称为“alegreyaregular.ttf”)

Typeface typeface = ResourcesCompat.getFont(this, R.font.alegreyaregular);
TextView textView.setTypeface(typeface);

I had exactly same error file name must end with .xml I used these settings and problem magically resolved.我有完全相同的错误文件名必须以 .xml 结尾我使用了这些设置并神奇地解决了问题。

Use these setting in Project Level Gradle :在 Project Level Gradle 中使用这些设置:

compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "com.company.app"
    minSdkVersion 21
    targetSdkVersion 26
...
}

Use these setting in Application Level Gradle :在 Application Level Gradle 中使用这些设置:

classpath 'com.android.tools.build:gradle:3.0.1'

Android Stidio :安卓斯蒂迪奥:

Android studio version 3.0.1.

Cheers!干杯!

I had faced same problem!!我遇到了同样的问题!! Just update your build.gradle file to resolve above issue.只需更新您的 build.gradle 文件即可解决上述问题。

classpath 'com.android.tools.build:gradle:3.0.1'类路径 'com.android.tools.build:gradle:3.0.1'

compileSdkVersion 26 compileSdkVersion 26

buildToolsVersion '26.0.3' buildToolsVersion '26.0.3'

Update your AndroidStudio to latest vetsion.将您的 AndroidStudio 更新到最新版本。

Enjoy your day.享受你的一天。

Android Studio 2.4 includes support for all the new developer features available with Android O you need to update studio Android Studio 2.4包含对Android O可用的所有新开发人员功能的支持,您需要更新 Studio

And create XML file并创建XML 文件

1) Right-click the font folder and go to New > Font resource file. 1) 右键单击​​字体文件夹,然后转到新建 > 字体资源文件。 The New Resource File window appears.出现“新建资源文件”窗口。

2) Enter the file name, and then click OK. 2) 输入文件名,然后单击“确定”。 The new font resource XML opens in the editor.新字体资源 XML 在编辑器中打开。

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/Montserrat_Regular.otf" />

</font-family>

This is kind of generic issue and I faced this one with error message这是一种通用问题,我遇到了错误消息

main\\res\\lib\\poi-3.12-android-a.jar: Error: The file name must end with .xml main\\res\\lib\\poi-3.12-android-a.jar: 错误:文件名必须以 .xml 结尾

I had placed this jar file inside resource folder(res), I removed this jar file and folder it get resolved.我已经把这个 jar 文件放在资源文件夹(res)中,我删除了这个 jar 文件和文件夹,它得到了解决。 Avoid any unwanted files/folder inside res directory.避免在 res 目录中出现任何不需要的文件/文件夹。

Fonts will go in the asset folder. 字体将进入资产文件夹。 Right click app folder in your studio -> New -> Folder -> Assets Folder, and the make a font folder in the assets folder 右键单击工作室中的app文件夹 - >新建 - >文件夹 - >资源文件夹,然后在资源文件夹中创建一个字体文件夹

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

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