更改 Material 组件样式属性失败

Changing Material Components style attributes fails

我正在尝试更改 Material Components Theme 的样式属性(如 colorPrimaryVariantcolorOnSecondary 等),但出现以下错误:

Android resource linking failed
Output:      C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3835: error: style attribute 'attr/colorPrimaryVariant (aka com.sample.app:attr/colorPrimaryVariant)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3836: error: style attribute 'attr/colorOnPrimary (aka com.sample.app:attr/colorOnPrimary)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:3839: error: style attribute 'attr/colorSecondaryVariant (aka com.sample.app:attr/colorSecondaryVariant)' not found.
C:\...\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.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 主题,但我得到了同样的错误。我仔细检查了我的依赖项,一切似乎都是正确的。我错过了什么?

我刚刚检查了 the documentation 和您使用的物品。它似乎仍未添加到 Theme.MaterialComponents 中,也不适用于该主题。

(也出现未找到错误(通过使用 Theme.MaterialComponents))

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

You can also incrementally test new Material components without changing your app theme. This allows you to keep your existing layouts looking and behaving the same, while introducing new components to your layout one at a time.

阅读: AppCompat Themes

看起来 1.1.0(目前处于 alpha 阶段)已经解决了这个问题:

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