代码之家  ›  专栏  ›  技术社区  ›  EGHDK asteri

更改Android中Switch的颜色

  •  16
  • EGHDK asteri  · 技术社区  · 12 年前

    我正在尝试在Android中更改开关的颜色。我意识到我需要新的9个补丁。我去了 http://android-holo-colors.com/ 选择了我的颜色并选择了(切换果冻豆)。要使用Switch Jelly Bean,我必须使用: https://github.com/BoD/android-switch-backport 。要将其导入我的项目,我必须添加:

    <item name="switchStyle">@style/Widget.Holo.CompoundButton.Switch</item>
    

    然后在xml中,我必须像这样使用开关:

    <org.jraf.android.backport.switchwidget.Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    

    现在开关一切正常。接下来,我将从android holo颜色生成器输出的所有内容放入适当的文件中:

    • 可绘制(2个选择器文件)
    • 可绘制hdpi(9个补丁文件)
    • 可绘制xhdpi(9个补丁文件)
    • 可绘制的xxhdpi(9个补丁文件)

    然后我添加到我的xml中:

    <org.jraf.android.backport.switchwidget.Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:thumb="@drawable/apptheme_switch_inner_holo_light"
    android:track="@drawable/apptheme_switch_track_holo_light" />
    

    但它仍然是原来的蓝色。我相信我做的一切都是正确的。一切都可以编译(xml、java)。注意:我正在导入 org.jraf.android.backport.switchwidget.Switch 在我的java中也是如此。有什么想法吗?

    6 回复  |  直到 6 年前
        1
  •  21
  •   amalBit    12 年前

    根据 this ,(通过BoD复制粘贴直接回答):

    • 您不得使用 android:thumb android:track 但相反, app:thumb app:track 并且必须将以下内容添加到xml文档的根目录中:

      xmlns:app="http://schemas.android.com/apk/res-auto"

        2
  •  6
  •   emen    10 年前

    在Android棒棒糖和更高版本中最简单的方式,

    <style name="AppTheme" parent="MaterialTheme.Light">
        ...
        <item name="android:colorControlActivated">@color/color_switch</item>
    </style>
    
        3
  •  3
  •   Mazhar Iqbal    4 年前

    用于androidx.swiccompact

                       app:thumbTint="@color/purple_700"
                        app:trackTint="@color/purple_700"
    
        4
  •  2
  •   sb_269    12 年前

    试试看:

    switch_thumb.xml

     <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_enabled="false" android:drawable="@drawable/switch_thumb_holo_light" />
            <item android:state_pressed="true"  android:drawable="@drawable/switch_thumb_activated_holo_light" />
            <item android:state_checked="true"  android:drawable="@drawable/switch_thumb_activated_holo_light" />
            <item                               android:drawable="@drawable/switch_thumb_holo_light" />
        </selector>
    

    在交换机布局中:

    android:thumb="@drawable/switch_thumb"
    
        5
  •  2
  •   Gavin    11 年前

    要更改开关的颜色,可以使用两个图像

    1. 绿色-开关打开时
    2. 红色-开关关闭时

    现在将此文件放在可绘制文件夹中

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

    在布局XML文件中,使用如下

    <CheckBox
        android:id="@+id/user_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/notif_checkbox_selector"
    />
    
        6
  •  1
  •   SDJMcHattie    12 年前

    也许你需要

    style="@style/switchStyle"
    

    在您的XML中