You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio示例项目扩展Material3样式时构建失败

问题:Material组件样式中app命名空间识别失败,资源链接错误

我创建了一个极简的Android Studio基础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>

Android Studio无法识别app命名空间,所有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.

补充信息

  • 平台:Mac x64,全新安装Android Studio Dolphin | 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'
}

解决方法

  • 移除属性的app:前缀:在样式文件中定义Material组件的属性时,不需要添加app:命名空间前缀,直接使用属性名即可。Material组件的属性在样式上下文里会自动关联到正确的命名空间,修改后的代码如下:
<resources>
    <style name="MyFab" parent="Widget.MaterialComponents.ExtendedFloatingActionButton">
        <item name="iconTint">?attr/colorOnBackground</item>
        <item name="fabSize">mini</item>
        <item name="tint">?attr/colorOnPrimary</item>
    </style>
</resources>
  • 确认应用主题为Material系列:检查AndroidManifest.xml中应用的主题是否继承自Material Components主题,例如Theme.MaterialComponents.DayNight.NoActionBar。如果使用的是AppCompat主题,可能会导致Material组件的属性无法被正确识别。
  • 文件名和位置无需修改:res/values/app_styles.xml的命名和存放位置是符合Android资源规范的,不需要调整,Android Studio会自动扫描该目录下的所有资源文件。

内容的提问来源于stack exchange,提问作者andbi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:10:39