简体   繁体   English

Android Material Chip 主题覆盖

[英]Android Material Chip Theme overriding

I want to customize a Material chip.我想定制一个材料芯片。

I would think this is how to do it:我认为这是如何做到的:

<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 of the tags like chipStyle affect the chips.chipStyle这样的标签都chipStyle影响筹码。 But if I set app:chipBackgroundColor="@color/chips" in xml it works.但是如果我在 xml 中设置app:chipBackgroundColor="@color/chips"就可以了。

It also works fine like this for other things like say <item name="materialAlertDialogTheme">@style/AlertDialogTheme</item> .它也适用于其他事情,例如<item name="materialAlertDialogTheme">@style/AlertDialogTheme</item>

The material documentation (if you can call it that) is really not helping.材料文档(如果你可以这么称呼的话)真的没有帮助。

Your app theme is correct.您的应用主题是正确的。

The default style used by Chip component is defined in the app theme by the chipStyle attribute . Chip组件使用的默认样式在应用主题中由chipStyle 属性定义

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

You can customize this style using for example:您可以使用例如以下方式自定义此样式:

<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>

If you specify the style attribute in your layout, this style overrides the default value.如果您在布局中指定style属性,则此样式将覆盖默认值。

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

In this case the Chip uses the Widget.MaterialComponents.Chip.Entry style.在这种情况下,Chip 使用Widget.MaterialComponents.Chip.Entry样式。

if you define chip style in layout xml, chip override your theme.如果您在布局 xml 中定义芯片样式,芯片将覆盖您的主题。 It may work if you clear chip style in layout xml.如果您清除布局 xml 中的芯片样式,它可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM