在我的应用中,第一个活动是选择器,我可以在其中选择3个按钮:红色,绿色,黄色。当我选择一个按钮时,将显示带有SwitchMaterial的新活动。现在,我想更改colorControlActivated,如果我选择了红色按钮,则开关颜色为红色,绿色按钮为绿色,依此类推。我该怎么办?
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/profile_setting_switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:theme="@style/SwitchTecnoTheme"
app:layout_constraintBottom_toBottomOf="@id/profile_setting_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/profile_setting_1" />
<style name="SwitchTecnoTheme" >
<item name="colorControlActivated">@color/tecno_brand_color</item>
</style>
答案 0 :(得分:0)
您可以定义3个不同的选择器,并使用类似的内容:
switch.isUseMaterialThemeColors = false
switch.thumbTintList = ContextCompat.getColorStateList(this,R.color.thumb_selector)
switch.trackTintList = ContextCompat.getColorStateList(this, R.color.track_selector)
其中thumb_selector
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/switchTrackDisable"/>
<item android:state_checked="true" android:color="@color/switchThumbActive" />
<item android:color="@color/switchThumbkNormal" />
</selector>
和track_selector
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="@color/switchTrackDisable"/>
<item android:state_checked="true" android:color="@color/switchTrackActive" />
<item android:color="@color/switchTrackNormal" />
</selector>