Android Material 芯片主题覆盖

Android Material Chip Theme overriding

我想定制一个Material芯片。

我想这是怎么做的:

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">

    .... lots more theme stuff here

    <item name="chipStyle">@style/MaterialChips</item>
    <item name="chipGroupStyle">@style/MaterialChips</item>
    <item name="chipStandaloneStyle">@style/MaterialChips</item>
</style>

<style name="MaterialChips" parent="Widget.MaterialComponents.Chip.Choice">
    <item name="chipBackgroundColor">@color/chips</item>
</style>

None 的标签如 chipStyle 影响筹码。但是如果我在 xml 中设置 app:chipBackgroundColor="@color/chips" 它就可以了。

它也适用于其他事情,比如 <item name="materialAlertDialogTheme">@style/AlertDialogTheme</item>

material 文档(如果您可以这样称呼它的话)确实没有帮助。

如果您在布局中定义芯片样式 xml,芯片会覆盖您的主题。 如果您在布局 xml.

中清除芯片样式,它可能会起作用

您的应用主题是正确的。

Chip component is defined in the app theme by the chipStyle attribute使用的默认样式

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  <!-- Default style for chip component -->
  <item name="chipStyle">@style/Widget.MaterialComponents.Chip.Action</item>  
</style>

您可以自定义此样式,例如:

<style name="AppTheme" parent="Theme.MaterialComponents.*">
  <!-- Default value for chipStyle -->
  <item name="chipStyle">@style/MaterialChips</item>  
</style>

<style name="MaterialChips" parent="@style/Widget.MaterialComponents.Chip.Choice">
    <!-- ... -->
    <item name="chipBackgroundColor">@color/chips</item>
</style>

如果您在布局中指定 style 属性,此样式 会覆盖 默认值。

        <com.google.android.material.chip.Chip
            style="@style/Widget.MaterialComponents.Chip.Entry"
            .../>

在这种情况下,芯片使用 Widget.MaterialComponents.Chip.Entry 样式。