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

自闭症网瘾萝莉.ら 提交于 2021-02-17 21:15:11

问题


Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources. 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.

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

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
2. Second Link
3. Third Link
4. Forth Link

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. But not getting the option of the Font resource file, please check below:


回答1:


Font resources are not fully supported by the Android Studio 2.3.3 build chain. Full support comes with Android Studio 3.0+ and its associated Android Plugin for Gradle version. At that point, you will be able to create a font/ resource directory that goes alongside layout/, menu/, etc.

IOW, hold off on this until you are ready to upgrade to Android Studio 3.0.




回答2:


I've lost an hour on this error, using AS 3.0 beta2, Gradle 4.x and the correct build-tools.

Turned out I had a copy of the fonts in a folder called "fonts". This folder doesn't show up in the Android project view. And reading the error I didn't catch the 's'. Just a small reminder for you readers!




回答3:


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.

  1. Right click 'res' folder in Android Studio

  2. Choose 'new' > 'Android resource directory'

  3. Name it 'font'

  4. Inhere you should place your font (.ttf) files WITH ONLY a-z 0-9 and _ characters.

  5. Set your font (mine is called 'alegreyaregular.ttf') this way:

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



回答4:


I had exactly same error file name must end with .xml I used these settings and problem magically resolved.

Use these setting in 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 :

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

Android Stidio :

Android studio version 3.0.1.

Cheers!




回答5:


I had faced same problem!! Just update your build.gradle file to resolve above issue.

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

compileSdkVersion 26

buildToolsVersion '26.0.3'

Update your AndroidStudio to latest vetsion.

Enjoy your day.




回答6:


Android Studio 2.4 includes support for all the new developer features available with Android O you need to update studio

And create XML file

1) Right-click the font folder and go to New > Font resource file. The New Resource File window appears.

2) Enter the file name, and then click OK. The new font resource XML opens in the editor.

<?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>



回答7:


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

I had placed this jar file inside resource folder(res), I removed this jar file and folder it get resolved. Avoid any unwanted files/folder inside res directory.



来源:https://stackoverflow.com/questions/45633502/getting-error-file-name-must-end-with-xml-while-using-the-androidfontfamily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!