您可以使用更改波纹的颜色
<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>