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

问题描述 投票:16回答:2

我试图改变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主题,但我得到了同样的错误。我仔细检查了我的依赖关系,一切似乎都是正确的。我错过了什么?

android android-theme material-components material-components-android
2个回答
21
投票

它看起来像1.1.0(目前在alpha中)解决了这个问题:

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

1
投票

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

(得到了未找到的错误(通过使用Theme.MaterialComponents))

有趣的是,我相信它应该与parent="Theme.AppCompat"合作,因为他们说它是测试它是如何工作的:

您还可以在不更改应用主题的情况下逐步测试新材料组件。这使您可以保持现有布局的外观和行为相同,同时一次向布局中引入新组件。

阅读:AppCompat Themes

© www.soinside.com 2019 - 2024. All rights reserved.