简体   繁体   English

如何更改SwitchCompat的轨道颜色

[英]How to change the track color of a SwitchCompat

I've tried using the following link to change the color of a SwitchCompat: 我尝试使用以下链接更改SwitchCompat的颜色:

How to change the color of a SwitchCompat 如何更改SwitchCompat的颜色

Notice the low constrast in my switch: 注意我的开关中的低对比度:

SwitchCompat

But after changing all relevant color values the track (the brighter grey) of the SwitchCompat remains the same. 但在更改所有相关颜色值后,SwitchCompat的轨道(亮灰色)保持不变。 I don't want to change the appearance except the color. 除了颜色,我不想改变外观。 The thumb is in pink, and I want the track to have some contrast. 拇指是粉红色的,我希望轨道有一些对比。 Did I miss to define a value in my styles.xml? 我错过了在styles.xml中定义一个值吗?

I tried these values (random non-white colors): 我尝试了这些值(随机非白色):

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/first</item>
    <item name="colorPrimaryDark">@color/second</item>
    <item name="colorAccent">@color/third</item>
   ...
    <item name="colorControlActivated">@color/first</item>
    <item name="colorControlHighlight">@color/first</item>
    <item name="colorControlNormal">@color/second</item>
    <item name="colorSwitchThumbNormal">@color/second</item>
    <item name="colorButtonNormal">@color/second</item>
...>

I had same probrem and solved it. 我有同样的问题并解决了它。

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
   ...
   <!-- Active thumb color & Active track color(30% transparency) -->
   <item name="colorControlActivated">@color/theme</item>
   <!-- Inactive thumb color -->
   <item name="colorSwitchThumbNormal">@color/grey300</item>
   <!-- Inactive track color(30% transparency) -->
   <item name="android:colorForeground">@color/grey600</item>
   ...
</style>

I read app compat code, and understand it. 我阅读app compat代码,并了解它。

android.support.v7.internal.widget.TintManager.java

private ColorStateList getSwitchTrackColorStateList() {
    if (mSwitchTrackStateList == null) {
        final int[][] states = new int[3][];
        final int[] colors = new int[3];
        int i = 0;

        // Disabled state
        states[i] = new int[] { -android.R.attr.state_enabled };
        colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.1f);
        i++;

        states[i] = new int[] { android.R.attr.state_checked };
        colors[i] = getThemeAttrColor(R.attr.colorControlActivated, 0.3f);
        i++;

        // Default enabled state
        states[i] = new int[0];
        colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.3f);
        i++;

        mSwitchTrackStateList = new ColorStateList(states, colors);
    }
    return mSwitchTrackStateList;
}

Below is the AppCompat way of changing both the track and thumb color programmatically , for a specific SwitchCompat . 以下是针对特定SwitchCompat以 编程方式更改轨道和拇指颜色的AppCompat方法。 For this example, I have hardcoded the thumbColor to red. 在这个例子中,我将thumbColor硬编码为红色。 Ideally, you would set the color through a second method parameter. 理想情况下,您可以通过第二个方法参数设置颜色。

Please note that when the switch is checked, a ripple is displayed. 请注意,检查开关时会显示纹波。 The ripple color will not be changed by the code below. 下面的代码不会改变纹波颜色。

public static void setSwitchColor(SwitchCompat v) {
    // thumb color of your choice
    int thumbColor = Color.RED;

    // trackColor is the thumbColor with 30% transparency (77)
    int trackColor = Color.argb(77, Color.red(thumbColor), Color.green(thumbColor), Color.blue(thumbColor));

    // setting the thumb color
    DrawableCompat.setTintList(v.getThumbDrawable(), new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_checked},
                    new int[]{}
            },
            new int[]{
                    thumbColor,
                    Color.WHITE
            }));

    // setting the track color
    DrawableCompat.setTintList(v.getTrackDrawable(), new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_checked},
                    new int[]{}
            },
            new int[]{
                    trackColor,
                    Color.parseColor("#4D000000") // full black with 30% transparency (4D)
            }));
}

if you want to costumize the color of track.you can use this solution. 如果你想要成本化轨道的颜色。你可以使用这个解决方案。

track selector.xml 跟踪selector.xml

 <?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/checked_color" android:state_checked="true" />
    <item android:color="@color/checked_color" android:state_selected="true" />
    <item android:color="@color/unchecked_color" android:state_checked="false" />
    <item android:color="@color/unchecked_color" android:state_selected="false" />

where checked_color and unchecked_color are color of your choices. 其中checked_color和unchecked_color是您选择的颜色。

styles.xml styles.xml

<style name="mySwitchStyle" parent="@style/Widget.AppCompat.CompoundButton.Switch">
       <!-- do here for additional costumization on thumb, track background,text appearance -->


    </style>


<style name="mySwitchTheme" parent="ThemeOverlay.AppCompat.Light">
        <item name="switchStyle">@style/mySwitchStyle</item>
        <item name="colorControlActivated">@color/red</item>
        <item name="colorControlNormal">@color/colorAccent</item>
        <item name="trackTint">@color/track_selector</item>
    </style>

layout file 布局文件

<android.support.v7.widget.SwitchCompat
        android:theme="@style/mySwitchTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
 Here is Switch Layout
 -Adding theme to your switch to change the color of track.Check the style given below:-.

**Switch Compact**
  <android.support.v7.widget.SwitchCompat
                android:id="@+id/goOnlineBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:theme="@style/Switch_style/>


**Switch_style**
   <style name="Switch_style" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="colorControlNormal">@android:color/white</item>
        <item name="colorControlActivated">@android:color/blue</item>
        <item name="colorSwitchThumbNormal">@android:color/white</item>
        <item name="trackTint">@color/white</item>
   </style>

Where trackTint will change your track color trackTint将改变您的曲目颜色

If you want a more switches in multiple colors in one Activity, you can use this solution (based on theme by @Konifar): 如果您想在一个Activity中使用多种颜色的更多开关,则可以使用此解决方案(基于@Konifar的主题):

<style name="CustomSwitchTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- Active thumb color & Active track color(30% transparency) -->
    <item name="colorControlActivated">@color/custom</item>
    <!-- Inactive thumb color -->
    <item name="colorSwitchThumbNormal">#E0E0E0</item>
    <!-- Inactive track color(30% transparency) -->
    <item name="android:colorForeground">#757575</item>
</style>

where @color/custom is color of thumb when switch is activated. 其中@color/custom是激活开关时的拇指颜色。

Then apply this theme to your SwitchCompat this way: 然后以这种方式将此主题应用于SwitchCompat:

<android.support.v7.widget.SwitchCompat
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/CustomSwitchTheme" />

I was having the same problem. 我遇到了同样的问题。 Finally solved it programatically with this Kotlin code 最后用这个Kotlin代码以编程方式解决了它

fun tintSwitchButton(sw: SwitchCompat, resolvedColor: Int) {
    val states = arrayOf(
            intArrayOf(-android.R.attr.state_pressed),
            intArrayOf(android.R.attr.state_pressed)
    )

    DrawableCompat.setTintList(sw?.trackDrawable, ColorStateList(
            states,
            intArrayOf(resolvedColor, resolvedColor)
    ))

    DrawableCompat.setTintList(sw?.thumbDrawable, ColorStateList(
            states,
            intArrayOf(Color.WHITE, Color.WHITE)
    ))
}

And the function call is 函数调用是

tintSwitchButton(switchCompat, Color.rgb(214, 0, 0))

You can also create an extension function: 您还可以创建扩展功能:

fun SwitchCompat.tint(resolvedColor: Int) {
    val states = arrayOf(
        intArrayOf(-android.R.attr.state_pressed),
        intArrayOf(android.R.attr.state_pressed)
    )

    DrawableCompat.setTintList(trackDrawable, ColorStateList(
        states,
        intArrayOf(resolvedColor, resolvedColor)
    ))

    DrawableCompat.setTintList(thumbDrawable, ColorStateList(
        states,
        intArrayOf(Color.WHITE, Color.WHITE)
    ))
}

So the call would be easier 所以电话会更容易

switchCompat.tint(Color.rgb(214,0,0))

just use colorControlActivated to set the color of SwitchCompat's track and thumb. 只需使用colorControlActivated来设置SwitchCompat的轨道和拇指的颜色。

If not set, the default colorControlActivated color will use colorAccent. 如果未设置,则默认的colorControlActivated颜色将使用colorAccent。 (from the experience, still not find where in the source code). (从经验来看,仍未找到源代码中的位置)。

The source code changed and does not still like @Ovidiu said. 源代码发生了变化,但仍然不喜欢@Ovidiu说。 But still thanks him for let me know to find the answer from the source code. 但仍然感谢他让我知道从源代码中找到答案。

mThumbDrawable = a.getDrawable(R.styleable.SwitchCompat_android_thumb);

will eventually call the following method. 最终将调用以下方法。

/frameworks/support/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java /frameworks/support/v7/appcompat/src/android/support/v7/widget/AppCompatDrawableManager.java

private ColorStateList createSwitchTrackColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;

    // Disabled state
    states[i] = ThemeUtils.DISABLED_STATE_SET;
    colors[i] = getThemeAttrColor(context, android.R.attr.colorForeground, 0.1f);
    i++;

    states[i] = ThemeUtils.CHECKED_STATE_SET;
    colors[i] = getThemeAttrColor(context, R.attr.colorControlActivated, 0.3f);
    i++;

    // Default enabled state
    states[i] = ThemeUtils.EMPTY_STATE_SET;
    colors[i] = getThemeAttrColor(context, android.R.attr.colorForeground, 0.3f);
    i++;

    return new ColorStateList(states, colors);
}

private ColorStateList createSwitchThumbColorStateList(Context context) {
    final int[][] states = new int[3][];
    final int[] colors = new int[3];
    int i = 0;

    final ColorStateList thumbColor = getThemeAttrColorStateList(context,
            R.attr.colorSwitchThumbNormal);

    if (thumbColor != null && thumbColor.isStateful()) {
        // If colorSwitchThumbNormal is a valid ColorStateList, extract the default and
        // disabled colors from it

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = thumbColor.getColorForState(states[i], 0);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = thumbColor.getDefaultColor();
        i++;
    } else {
        // Else we'll use an approximation using the default disabled alpha

        // Disabled state
        states[i] = ThemeUtils.DISABLED_STATE_SET;
        colors[i] = getDisabledThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
        i++;

        states[i] = ThemeUtils.CHECKED_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorControlActivated);
        i++;

        // Default enabled state
        states[i] = ThemeUtils.EMPTY_STATE_SET;
        colors[i] = getThemeAttrColor(context, R.attr.colorSwitchThumbNormal);
        i++;
    }

    return new ColorStateList(states, colors);
}

SwitchCompat is a Material Design widget. SwitchCompat是Material Design小部件。 when my activity extends AppCompatActivity and android:theme="@style/mySwitch" is worked. 当我的活动扩展AppCompatActivity和android:theme =“@ style / mySwitch”工作。

Here is how a developer can change the track drawable of a SwitchCompat: 以下是开发人员如何更改SwitchCompat的轨道绘制:
First, in the root layout, write xmlns:SwitchCompat="http://schemas.android.com/apk/res-auto" 首先,在根布局中,编写xmlns:SwitchCompat="http://schemas.android.com/apk/res-auto"
Then: 然后:

    <android.support.v7.widget.SwitchCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:thumb="@drawable/your_switch_thumb"
        SwitchCompat:track="@drawable/your_switch_track_selector" 
        />

where your_switch_track_selector can be: your_switch_track_selector可以是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/switch_ios_track_on" android:state_checked="true" />
    <item android:drawable="@drawable/switch_ios_track_off" android:state_checked="false" />
</selector>

1.switch_ios_track_on:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="#4EDA62" />
    <corners android:radius="20dp" />
</shape>

2.switch_ios_track_off:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="#E3E3E3" />
    <corners android:radius="20dp" />
</shape>

Simple and easy.. 简单易用..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM