使用Android O的XML字体

时间:2017-06-15 07:09:02

标签: android android-studio android-8.0-oreo

Android O给新的android-studio版本3.0的字体目录,但是当在字体目录中的drop font-file并运行项目时,它会在字体目录中的字体文件中给出错误。 错误:任务':app:mergeDebugResources'执行失败。

  

/home/ttuser4/Downloads/fontPractice/app/src/main/res/font/Dancing_Script.ttf:错误:文件名必须以.xml结尾

4 个答案:

答案 0 :(得分:12)

我也遇到了类似的问题。为了解决这个问题,我按照以下步骤进行了修复。

  1. 将Android Studio更新为最新的canary版本。
  2. distributionUrl中的project-dir/gradle/gradle-wrapper.properties更改为最新的gradle分发网址(您可以从here获取最新的gradle可用性)以更新用于构建的gradle版本,如下所示。
      

    distributionUrl = HTTPS://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip

  3. 然后将根com.android.tools.build:gradle中的build.gradle版本更新为最新版本,如下所示。 (注意:下面提到的版本是答案发布时的最新版本。版本可能会在未来发生变化。请注意这一点。)
  4. buildscript {
        repositories {
            jcenter()
            maven {
                url 'https://maven.google.com'
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            maven {
                url 'https://maven.google.com'
            }
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    1. 同步,清理和更新项目。
    2. 它对我有用。请告诉我你的意见。

答案 1 :(得分:2)

我假设您使用的是较旧的构建工具插件,因此它不了解res / font和新的字体功能。

我遇到了同样的问题,我更新为gradle构建工具2.4.0-alpha7

但我在构建时收到错误消息,然后必须设置环境变量

launchctl setenv ANDROID_DAILY_OVERRIDE <your-value-on-error-message>

答案 2 :(得分:0)

假设您已经打开了Android Studio项目。

  1. 右键点击res目录,然后选择New > Android Resource Directory

  2. 选择资源目录“font”。

  3. enter image description here

    1. 将您的字体名称更改为“dancing.ttf”(大写名称生成错误)。我使用“Pacifico-Font”并收到以下错误消息 -
    2.   

      错误:任务':app:mergeDebugResources'的执行失败。       res / font / Pacifico-Font.ttf:错误:'P'不是有效的基于文件的资源名称字符:基于文件的资源名称必须仅包含   小写a-z,0-9或下划线

      当我将其更改为pacifico.ttf时,它有效。

      1. 将字体添加到fonts文件夹中。
      2. 运行您的应用。
      3. 我在Android Studio 3.0 Preview中使用“pacifico.ttf”字体执行完全相同的步骤,它对我有用。

        enter image description here

        如果您遇到任何错误,请清理一次项目,然后再次运行。

        如果您仍然遇到问题,请告诉我。

答案 3 :(得分:-2)

您可以添加任何格式的字体,但您必须创建Font Family xml文件才能在xml布局文件中应用这些字体。字体系列是一个XML文件,包含多个字体文件及其样式和重量详细信息。您可以将字体系列作为一个单元访问。

要创建字体系列,请在Android Studio中执行以下步骤:

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

  2. 输入文件名,然后单击“确定”。新的字体资源XML将在编辑器中打开。

  3. 将元素中的每个字体文件,样式和权重属性括起来。以下XML说明了在字体资源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/lobster_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/lobster_italic" />
    </font-family>
    
  4. 然后,您可以将此font-family添加到包含文本的布局中的任何元素。就像在这个例子中它适用于TextView

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lobster"/>