Android 尝试在 styles.xml 中使用自定义字体时资源链接失败

Android resource linking failed when trying to use custom font in styles.xml

我正在尝试使用具有新 "android:fontFamily" 属性的自定义字体,该属性已添加到 Android O 中。虽然这在 xml:

中有效
android:fontFamily="@font/sf_pro_text_semibold"

在 styles.xml 中使用相同的构造,如下所示:

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>

导致以下构建错误:

Execution failed for task ':app:processDebugResources'.

Android resource linking failed C:\Users\anro.gradle\caches\transforms-2\files-2.125ed89f50d00591f7f19265a14b826\res\values\values.xml: AAPT: error: resource font/sf_pro_text_semibold" (aka com.example.fonttesting:font/sf_pro_text_semibold") not found.

很多教程都说它应该有效,但实际上没有。我想问题出在 build.gradle 文件中,但我什至不知道如何让它工作(降级到旧的支持库版本不是一个好的解决方案)。这是我的 build.gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.3'
    defaultConfig {
        applicationId "com.example.fonttesting"
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

那么,有人知道如何在 styles.xml 中使用 androidx 制作自定义字体吗?

AAPT: error: resource font/sf_pro_text_semibold" (aka com.example.fonttesting:font/sf_pro_text_semibold") not found.

有一个错字:

删除 " 字符:

<item name="android:fontFamily">@font/sf_pro_text_semibold</item>

而不是

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>

我遇到了类似的问题,我的问题得到了扩展

<item name="android:fontFamily">@font/muli.ttf</item>
<item name="fontFamily">@font/muli.ttf</item>

删除扩展后它对我有用。

<item name="android:fontFamily">@font/muli</item>
<item name="fontFamily">@font/muli</item>