更改材质组件样式属性失败



我正在尝试从材质组件主题更改样式属性(如colorPrimaryVariantcolorOnSecondary等(,但出现以下错误:

Android resource linking failed
Output:      C:...appbuildintermediatesincrementalmergeDebugResourcesmerged.dirvaluesvalues.xml:3835: error: style attribute 'attr/colorPrimaryVariant (aka com.sample.app:attr/colorPrimaryVariant)' not found.
C:...appbuildintermediatesincrementalmergeDebugResourcesmerged.dirvaluesvalues.xml:3836: error: style attribute 'attr/colorOnPrimary (aka com.sample.app:attr/colorOnPrimary)' not found.
C:...appbuildintermediatesincrementalmergeDebugResourcesmerged.dirvaluesvalues.xml:3839: error: style attribute 'attr/colorSecondaryVariant (aka com.sample.app:attr/colorSecondaryVariant)' not found.
C:...appbuildintermediatesincrementalmergeDebugResourcesmerged.dirvaluesvalues.xml:3840: error: style attribute 'attr/colorOnSecondary (aka com.sample.app:attr/colorOnSecondary)' not found.
error: failed linking references.

这是我的主题的样子:

<resources>
<!-- Light application theme -->
<style name="CBTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/cbPrimary</item>
<item name="colorPrimaryDark">@color/cbPrimaryDark</item>
<item name="colorPrimaryVariant">@color/cbPrimaryDark</item>
<item name="colorOnPrimary">#FFFFFF</item>
<item name="colorAccent">@color/cbAccent</item>
<item name="colorSecondary">@color/cbAccent</item>
<item name="colorSecondaryVariant">@color/cbAccentLight</item>
<item name="colorOnSecondary">#FFFFFF</item>
<item name="colorError">@color/cbRed</item>
<item name="android:windowBackground">@color/lightThemeBackground1</item>
</style>
[...]
</resources>

如果我不添加四个受影响的属性,一切正常。我的minSdkVersion是16,compileSdkVersion和targetSdkVersion是28。我也尝试使用 Bridge 主题,但出现同样的错误。我仔细检查了我的依赖项,一切似乎都是正确的。我错过了什么?

看起来1.1.0(目前为 alpha(已经解决了这个问题:

implementation 'com.google.android.material:material:1.1.0-alpha09'

我刚刚检查了文档和您使用的项目。似乎它仍然没有添加到Theme.MaterialComponents中,也不适用于该主题。

(也收到"找不到"错误(通过使用Theme.MaterialComponents

(有趣的是,我相信它应该适用于parent="Theme.AppCompat",因为他们说这是为了测试看看它是如何工作的:

您还可以增量测试新的材质组件,而无需 更改应用主题。这使您可以保留现有布局 外观和行为相同,同时引入新组件 您的布局一次一个。

阅读:应用兼容主题

colorSurface 用于材料的"片材"(如卡片和底片(。

您不必覆盖所有颜色。有些(如 colorSurface(使用中性色,因此依赖默认值是完全可以的。

资源 : https://material.io/blog/android-material-theme-color