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

anr*_*nro 1 android android-gradle-plugin

我正在尝试使用在 Android O 中添加的具有新“android:fontFamily”属性的自定义字体。虽然这在 xml 中有效:

android:fontFamily="@font/sf_pro_text_semibold"
Run Code Online (Sandbox Code Playgroud)

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

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>
Run Code Online (Sandbox Code Playgroud)

导致以下构建错误:

Execution failed for task ':app:processDebugResources'.
Run Code Online (Sandbox Code Playgroud)

Android 资源链接失败 C:\Users\anro.gradle\caches\transforms-2\files-2.1\4125ed89f50d00591f7f19265a14b826\res\values\values.xml:AAPT:错误:资源字体/sf_pro_text_semibold.example.字体/sf_pro_text_semibold") 未找到。

很多教程都说它应该可以工作,但事实并非如此。我想问题出在 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'
}
Run Code Online (Sandbox Code Playgroud)

那么,有人知道如何使styles.xml 中的自定义字体与androidx 配合使用吗?

Gab*_*tti 6

AAPT:错误:找不到资源字体/sf_pro_text_semibold”(又名 com.example.fonttesting:font/sf_pro_text_semibold”)。

有一个错字:

删除"字符:

<item name="android:fontFamily">@font/sf_pro_text_semibold</item>
Run Code Online (Sandbox Code Playgroud)

代替

<item name="android:fontFamily">@font/sf_pro_text_semibold"</item>
Run Code Online (Sandbox Code Playgroud)

  • 我不敢相信我犯了这样一个愚蠢的错误并且无法发现它。非常感谢! (2认同)