Material 组件未使用主题中声明的颜色强调

Material components not using color accent declared in theme

我的主要应用主题父项是 MaterialComponents 主题。我更改了颜色,但是 material 组件(MaterialButton、InputLayout、EditText)没有使用我的强调色(它们使用了一些蓝色,我声明的强调色是蓝绿色) 问题是什么?如何处理 Material 组件的最佳方式?

我的主题:

    <style name="AppTheme" parent="Theme.MaterialComponents">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

编辑: 将主题更改为 Theme.MaterialComponents.Bridge 并不能解决该问题。

Theme.AppCompat 继承您的应用主题,例如:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ...
</style>

请注意,您可以使用深色或浅色主题,但您需要扩展 Theme.AppCompat

尝试像这样更改您的 styles.xml :

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

如果可行,请告诉我

编辑:尝试按照此 link 设置 material 组件 https://medium.com/@pedromassango/android-material-design-components-setup-google-i-o18-part-1-8894f315b5e

我的问题是针对特定 API 声明的不同 styles.xml。我在其中犯了一个错误,在测试时,在 styles.xml.

之前应用了其他 values-v21/styles.xml

长话短说,检查其他变体中是否不存在该错误。我声明那个变体是错误的。

也许你应该尝试使用 colorSecondary 而不是 colorAccent:

 <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- Original AppCompat attributes. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorSecondary">@color/colorAccent</item>
</style>

这对我有用

对于那些尝试使用继承自 Theme.MaterialComponents.* 的主题的人,请记住不要使用常规 Button class,而是使用 com.google.android.material.button.MaterialButton

而不是:

<Button
  (...)
  style="@style/Widget.MaterialComponents.Button" />

确保使用:

<com.google.android.material.button.MaterialButton
  (...)
  style="@style/Widget.MaterialComponents.Button" />

去过那里。没什么好羞愧的。 :)