【问题标题】:How to tint the color of the shadow in Android Switch?如何在 Android Switch 中着色阴影的颜色?
【发布时间】:2021-12-26 18:11:33
【问题描述】:

我用的是Material Switch,我知道如何改变轨道和拇指的颜色,但是如何改变辐射阴影的颜色呢?

我已将颜色更改为紫色,但当我点击它时,仍然有那个绿色阴影。我尝试了样式、appcompat 开关等,但没有任何效果。

这是我的代码:

    <com.google.android.material.switchmaterial.SwitchMaterial
    android:id="@+id/switch_compat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:trackTint="@color/purple_500"
    app:thumbTint="@color/purple_700"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

谢谢!

【问题讨论】:

标签: android colors


【解决方案1】:

您可以使用更改波纹的颜色

<item name="colorControlHighlight">@color/purple_700</item>

但是您需要在styles.xml 或themes.xml 中为此定义两种样式:

<style name="SelectableItemBackgroundBorderless">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackgroundBorderless</item>
</style>

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/purple_700</item>
</style>

然后为你的 switch 定义 style 属性:

 style="@style/SelectableItemBackgroundBorderless"

所以你的 Switch 将是:

<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switch_compat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trackTint="@color/purple_500"
app:thumbTint="@color/purple_700"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
style="@style/SelectableItemBackgroundBorderless" />

编辑:

您可以在关闭状态和开启状态下更改波纹的颜色。无需使用第一种样式。只需定义主题,无需定义样式:

android:theme="@style/SelectableItemTheme"

在样式中定义:

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/purple_500</item>
    <item name="colorControlActivated">@color/purple_700</item>
</style>

【讨论】:

  • 谢谢!你拯救了这一天。请问你这是在哪里找到的?我正在查看样式,但没有注意到这一点。
  • 我不记得了。但我使用这种风格已经有一段时间了。一定是从另一个 stackoverflow 问题中找到的...
猜你喜欢
  • 2015-07-14
  • 2012-09-18
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
相关资源
最近更新 更多