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

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

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


当前回答

<androidx.appcompat.widget.SwitchCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:thumbTint="@color/white"
                app:trackTint="@drawable/checker_track"/>

在checker_track.xml中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/lightish_blue" android:state_checked="true"/>
    <item android:color="@color/hint" android:state_checked="false"/>
</selector>

其他回答

虽然SubChord的回答是正确的,但它并没有真正回答如何在不影响其他小部件的情况下设置“打开”颜色的问题。要做到这一点,在styles.xml中使用ThemeOverlay:

<style name="ToggleSwitchTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="colorAccent">@color/green_bright</item>
</style>

并在你的开关中引用它:

<android.support.v7.widget.SwitchCompat
  android:theme="@style/ToggleSwitchTheme" ... />

这样做只会影响你想要应用它的视图的颜色。

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

 android:thumb="your drawable here"

派对来晚了,但我就是这么做的

风格

 <style name="SCBSwitch" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="colorControlActivated">#46bdbf</item>

        <!-- inactive thumb color -->
        <item name="colorSwitchThumbNormal">#f1f1f1
        </item>

        <!-- inactive track color (30% transparency) -->
        <item name="android:colorForeground">#42221f1f
        </item>
    </style>

颜色

布局

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:checked="false"
    android:theme="@style/SCBSwitch" />

结果

请参见颜色变化的启用和禁用开关

Android Studio 3.6解决方案:

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

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

创建可绘制的newthumb。xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/Green" android:state_checked="true"/>
  <item android:color="@color/Red" android:state_checked="false"/>
</selector>

创建可绘制的newtrack。xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/black" android:state_checked="true"/>
  <item android:color="@color/white" android:state_checked="false"/>
</selector>

并添加到Switch中:

<Switch
  android:trackTint="@drawable/newtrack"
  android:thumbTint="@drawable/newthumb"
/>

使用app:trackTint和app:thumbTint来切换compat androidx -见@Ehsan Rosdi的评论。

此外,它完全可以只做一个可绘制文件(“switchcolors.xml”),并将其用于trackTint和thumbTint。