android - 如何在 Android Switch 中为阴影着色?

标签 android colors

我正在使用 Material Switch,我知道如何更改轨道和拇指的颜色,但是如何更改辐射阴影的颜色?
enter image description here
我已将颜色更改为紫色,但是当我点击它时,仍然有那个绿色阴影。我尝试了样式、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" />
谢谢!

最佳答案

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

<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>
然后为您的开关定义样式属性:
 style="@style/SelectableItemBackgroundBorderless"
所以你的开关将是:
<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" />
编辑:
您可以在 OFF 状态和 ON 状态更改波纹的颜色。无需使用第一种样式。只需定义主题,无需定义样式:
android:theme="@style/SelectableItemTheme"
在样式中定义:
<style name="SelectableItemTheme">
    <item name="colorControlHighlight">@color/purple_500</item>
    <item name="colorControlActivated">@color/purple_700</item>
</style>

关于android - 如何在 Android Switch 中为阴影着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69976056/

相关文章:

Android 以编程方式将联系人设置为收藏夹

java - 记录耳机输出 : Android

android - 安卓国际化

javascript - 如何根据列值给DC js气泡图着色?

javascript - 在 D3.js 树的中心和不同节点之间绘制不同颜色的 "strokes"

java - Android : Show a list of color previews and pass associated id from color. xml

android - MediaCodec 和摄像头 : colorspaces don't match

android - 如何在 Android 中干净地处理多个数据库表?

Android 我的应用程序是否存在内存泄漏?

c++ - Directx 顶点颜色