代码之家  ›  专栏  ›  技术社区  ›  AskQ

无法更改开关颜色

  •  10
  • AskQ  · 技术社区  · 7 年前

    我正在寻找应用此颜色的所有开关只。但默认情况下,它需要 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>
    

    我做错了什么?

    3 回复  |  直到 7 年前
        1
  •  20
  •   Eugen Pechanec    3 年前

    你在混音 样式 主题

    这些属性是 因此,在主题覆盖中一起定义它们:

    (非值-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>
    

    然后在交换机上应用此主题覆盖:

    资源/布局/布局。xml

    <Switch
        android:theme="@style/ThemeOverlay.MySwitch"/>
    
    <androidx.appcompat.widget.SwitchCompat
        android:theme="@style/ThemeOverlay.MySwitchCompat"/>
    

    从两种变体中选择一种:

    • Switch 自API 21开始提供,所有主题属性的前缀都是 android:
    • SwitchCompat 在AndroidX AppCompat库中可用,一些主题属性没有前缀(请确保您知道是哪个)。
        2
  •  4
  •   Boris Kozyrev    7 年前

    也许,你可以尝试使用 android.support.v7.widget.SwitchCompat 而不是 Switch android:theme=@style/SwitchStyle style="@style/SwitchStyle"

        3
  •  3
  •   Eugen Pechanec    7 年前

    将此添加到样式中。用于交换机样式的xml。

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

    在xml的使用中

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