Android Material Chip 主题覆盖

smd*_*ufb 5 android material-design android-chips material-components material-components-android

我想定制一个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>
Run Code Online (Sandbox Code Playgroud)

像这样的标签都不会chipStyle影响芯片。但如果我app:chipBackgroundColor="@color/chips"在 xml 中设置它就可以了。

对于其他事情,比如 say ,它也可以很好地工作<item name="materialAlertDialogTheme">@style/AlertDialogTheme</item>

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

Gab*_*tti 3

您的应用程序主题是正确的。

组件使用的默认样式由应用程序主题中的属性Chip定义。chipStyle

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  <!-- Default style for chip component -->
  <item name="chipStyle">@style/Widget.MaterialComponents.Chip.Action</item>  
</style>
Run Code Online (Sandbox Code Playgroud)

您可以使用例如以下方式自定义此样式:

<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>
Run Code Online (Sandbox Code Playgroud)

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

        <com.google.android.material.chip.Chip
            style="@style/Widget.MaterialComponents.Chip.Entry"
            .../>
Run Code Online (Sandbox Code Playgroud)

在本例中,Chip 使用该Widget.MaterialComponents.Chip.Entry样式。