How to change the track color of a SwitchCompat

后端 未结 10 1874
孤城傲影
孤城傲影 2020-11-29 18:01

I\'ve tried using the following link to change the color of a SwitchCompat:

How to change the color of a SwitchCompat

Notice the low constrast in my switch:<

10条回答
  •  孤街浪徒
    2020-11-29 18:31

    I had same probrem and solved it.

    
    

    I read app compat code, and understand it.

    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;
    }
    

提交回复
热议问题