无法更改开关颜色

问问

我正在寻找将此颜色仅应用于所有开关。但默认情况下,它将采用colorAccent此主题代替此主题。

装置:棉花糖。

布局:

<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"
            />

样式-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>

我究竟做错了什么?

尤金·佩查内克(Eugen Pechanec)

您正在将样式主题混合在一起。

这些属性是主题属性,因此可以在主题叠加层中一起定义它们:

res / values / styles.xml(不是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>

然后在开关上应用此主题叠加层:

res / layout / layout.xml

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

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

选择以下两种变体之一:

  • Switch 自API 21开始可用,所有主题属性均以前缀 android:
  • SwitchCompat 在appcompat-v7支持库中可用,某些主题属性没有前缀(请确保您知道哪个)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章