【问题标题】:Android Material Chip Theme overridingAndroid Material Chip 主题覆盖
【发布时间】:2020-03-27 11:55:15
【问题描述】:

我想定制一个 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>

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

它也适用于像 &lt;item name="materialAlertDialogTheme"&gt;@style/AlertDialogTheme&lt;/item&gt; 这样的其他事情。

材料文档(如果你可以这么说的话)真的没有帮助。

【问题讨论】:

  • 样式由chipStyle 属性定义。有用。布局中如何定义Chip?
  • 我猜你怀疑是这样的。我在筹码上设置了style="@style/Widget.MaterialComponents.Chip.Choice"...我觉得很傻。谢谢你把我带到那里。

标签: android material-design material-components material-components-android android-chips


【解决方案1】:

您的应用主题正确。

Chip 组件使用的默认样式在应用主题中由 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 样式。

【讨论】:

    【解决方案2】:

    如果您在布局 xml 中定义芯片样式,芯片会覆盖您的主题。 如果您在布局xml中清除芯片样式,它可能会起作用。

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 2020-12-20
      • 2021-12-19
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      相关资源
      最近更新 更多