我正在使用 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" />
谢谢!
最佳答案
您可以使用更改波纹的颜色
<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/