Android Studio Studio的演示项目在扩展Material3样式时失败

hsvhsicv  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(137)

我已经创建了一个非常简单的Android Studio启动项目(Basic Activity),在res/values/app_styles.xml中添加简单的样式文件后,我收到了链接错误:

<resources>
    <style name="MyFab" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
        <item name="iconTint">?attr/colorOnBackground</item>
        <item name="app:fabSize">mini</item>
        <item name="app:tint">?attr/colorOnPrimary</item>
    </style>
</resources>

app名称空间似乎不为Android Studio所知,每个app:**项都以红色突出显示,并且构建失败并出现链接错误:

Android resource linking failed
xxx.myapp.app-mergeDebugResources-43:/values-w600dp-v13/values-w600dp-v13.xml:6: error: style attribute 'app:attr/fabSize' not found.
xxx.myapp.app-mergeDebugResources-43:/values-w600dp-v13/values-w600dp-v13.xml:7: error: style attribute 'app:attr/tint' not found.
error: failed linking references.

我在这里做错了什么?我应该使用不同的文件名/位置吗?
PS:Android Studio 海豚|Mac x64上的2021.3.1 Patch 1,全新安装。
项目依赖关系:

dependencies {
    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
qvtsj1bj

qvtsj1bj1#

必须删除style定义中属性名称的app前缀。
请注意,ExtendedFloatingActionButtonMaterialButton的子类,而不是FloatingActionButton。这意味着适用于FloatingActionButton的几个属性在ExtendedFloatingActionButton中有不同的命名或不存在。
对于ExtendedFloatingActionButton,不存在fabSizetint

<style name="MyFab" parent="Widget.MaterialComponents.FloatingActionButton">
    <item name="iconTint">?attr/colorOnBackground</item>
    <item name="fabSize">mini</item>
    <item name="tint">?attr/colorOnPrimary</item>
</style>

相关问题