Unable to change switch color

孤者浪人 提交于 2019-12-12 15:45:11

问题


I'm looking for applying this color to all switches only. But by default, it is taking colorAccent instead of this theme for switch.

Device: marshmallow.

layout:

<Switch
            android:id="@+id/soundSwitch"
            style="@style/SwitchStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginBottom="@dimen/large_space"
            android:layout_marginRight="@dimen/medium_space"
            android:layout_marginTop="@dimen/large_space"
            android:checked="true"
            />

styles-v21:

<style name="SwitchStyle" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="android:colorControlActivated">@color/switch_color</item>

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

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

What am I doing wrong?


回答1:


You're mixing styles and themes together.

These attributes are theme attributes so define them together in a theme overlay:

res/values/styles.xml (not values-v21)

<style name="ThemeOverlay.MySwitch" parent="">
    <item name="android:colorControlActivated">@color/switch_color</item>
    <item name="android:colorSwitchThumbNormal">#f1f1f1</item>
    <item name="android:colorForeground">#42221f1f</item>
</style>

<style name="ThemeOverlay.MySwitchCompat" parent="">
    <item name="colorControlActivated">@color/switch_color</item>
    <item name="colorSwitchThumbNormal">#f1f1f1</item>
    <item name="android:colorForeground">#42221f1f</item>
</style>

And then apply this theme overlay on the switch:

res/layout/layout.xml

<Switch android:theme="@style/ThemeOverlay.MySwitch"/>

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

Pick one of the two variants:

  • Switch available since API 21, all theme attributes are prefixed with android:
  • SwitchCompat available in appcompat-v7 support library, some theme attributes are not prefixed (make sure you know which).



回答2:


Probably, you can try using android.support.v7.widget.SwitchCompat instead of Switch and android:theme=@style/SwitchStyle instead of style="@style/SwitchStyle"




回答3:


Add this to style.xml for switch styling.

<style name="SwitchThemeOverlay" parent="">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">#00c853</item>

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

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

and in your xml use

 <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_desc"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/colorPrimaryDark"
        android:padding="5dp"
        android:checked="false"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
        android:theme="@style/SwitchThemeOverlay"
        android:layout_marginLeft="10dp"
        android:text="Description"/>


来源:https://stackoverflow.com/questions/47035777/unable-to-change-switch-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!