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

问题描述 投票:14回答:7

Android O引入了一项新功能,即XML字体,可让您将字体用作资源。我正在资源文件中创建font文件夹,因为它由Android developer提供,但问题是我在使用Android O版本中提供的file name must end with .xml文件夹时收到font的错误。

请检查以下布局。

 <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" />

请查看font文件夹,该文件夹是我在res中使用Montserrat_Regular.otf文件创建的

My font folder with font style

使用上面的内容时出现以下错误,如下所示:

Error getting

我搜索了它但没有得到预期的结果,请检查:

1. First Link 2. Second Link 3. Third Link 4. Forth Link

并且Font resource file没有创建,因为我点击Right-click the font folder and go to New > Font resource file。但是没有获得Font resource file的选项,请查看以下内容:

enter image description here

android layout fonts android-resources android-8.0-oreo
7个回答
9
投票

Android Studio 2.3.3构建链不完全支持字体资源。完全支持Android Studio 3.0(目前仅在Canary频道上提供)及其相关的Gradle版Android插件。

IOW,请等待,直到您准备好升级到Android Studio 3.0。

(顺便说一下,当你看到“Android Studio 2.4”的引用时,几个月前它被重命名为Android Studio 3.0)


3
投票

我遇到了同样的问题,在寻找答案的时候,我来到了这个主题。我找到了deverloper.android.com的解决方案。

  1. 右键单击Android Studio中的“res”文件夹
  2. 选择“新”>“Android资源目录”
  3. 将其命名为'font'
  4. 在某种程度上,您应该将字体(.ttf)文件仅包含在a-z 0-9和_字符中。
  5. 以这种方式设置你的字体(我的名字叫'alegreyaregular.ttf'):
Typeface typeface = ResourcesCompat.getFont(this, R.font.alegreyaregular);
TextView textView.setTypeface(typeface);

2
投票

我使用AS 3.0 beta2,Gradle 4.x和正确的构建工具在这个错误上丢失了一个小时。

原来我在一个名为“fonts”的文件夹中有一份字体副本。此文件夹未显示在Android项目视图中。阅读错误我没有抓住's'。只是提醒您的读者!


2
投票

我有完全相同的错误文件名必须以.xml结尾我使用这些设置和问题神奇地解决了。

在Project Level Gradle中使用这些设置:

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

在应用程序级别Gradle中使用这些设置:

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

Android Studio:

Android studio version 3.0.1.

干杯!


1
投票

Android Studio 2.4包含对Android O所需的所有新开发人员功能的支持,您需要更新工作室

并创建XML文件

1)右键单击字体文件夹,然后转到“新建”>“字体资源文件”。将出现“新建资源文件”窗口。

2)输入文件名,然后单击“确定”。新的字体资源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>

1
投票

我遇到过同样的问题!!只需更新build.gradle文件即可解决上述问题。

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

compileSdkVersion 26

buildToolsVersion '26 .0.3'

将您的Android Studio更新到最新版本。

祝您愉快。


-2
投票

字体将进入资产文件夹。右键单击工作室中的app文件夹 - >新建 - >文件夹 - >资源文件夹,然后在资源文件夹中创建一个字体文件夹

© www.soinside.com 2019 - 2024. All rights reserved.