我正在使用一个标准的开关控制与全息。轻主题在ICS应用程序。

我想改变切换按钮的高亮或状态颜色,从标准的浅蓝色到绿色。

这应该很容易,但我似乎不知道该怎么做。


当前回答

在xml中,你可以这样改变颜色:

 <androidx.appcompat.widget.SwitchCompat
    android:id="@+id/notificationSwitch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    app:thumbTint="@color/darkBlue"
    app:trackTint="@color/colorGrey"/>

您可以动态更改为:

Switch.thumbDrawable.setColorFilter(ContextCompat.getColor(requireActivity(), R.color.darkBlue), PorterDuff.Mode.MULTIPLY)

其他回答

Android Studio 3.6解决方案:

yourSwitch.setTextColor(getResources().getColor(R.color.yourColor));

更改颜色XML文件定义值(yourColor)中的文本颜色。

改变轨道和拇指绘制的色调颜色。

switch.getThumbDrawable().setTint(ContextCompat.getColor(this,R.color.colorAccent));

switch.getTrackDrawable().setTint(ContextCompat.getColor(this,R.color.colorAccent));

创建自己的9-patch图像,并将其设置为切换按钮的背景。

http://radleymarx.com/2011/simple-guide-to-9-patch/

在xml中,你可以这样改变颜色:

 <androidx.appcompat.widget.SwitchCompat
    android:id="@+id/notificationSwitch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    app:thumbTint="@color/darkBlue"
    app:trackTint="@color/colorGrey"/>

您可以动态更改为:

Switch.thumbDrawable.setColorFilter(ContextCompat.getColor(requireActivity(), R.color.darkBlue), PorterDuff.Mode.MULTIPLY)

可能有点晚了,但是对于开关按钮,toogle按钮不是答案,你必须改变开关xml参数中的drawable:

 android:thumb="your drawable here"