【问题标题】:Change color of Switch in Android在 Android 中更改 Switch 的颜色
【发布时间】:2014-02-12 16:54:31
【问题描述】:

我正在尝试更改 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 个选择器文件)
  • drawable-hdpi(9个补丁文件)
  • drawable-xhdpi(9个补丁文件)
  • drawable-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)。注意:我也在我的 java 中导入org.jraf.android.backport.switchwidget.Switch。有什么想法吗?

【问题讨论】:

  • 看看这个帖子,解释的很好[android中的样式开关][1][1]:stackoverflow.com/questions/10118050/…
  • 我已经看过了。 Android 全息颜色生成正确的 9 个补丁图像和 2 个可绘制选择器文件,它们肯定在其中,但没有显示出来。

标签: java android xml android-theme android-styles


【解决方案1】:

根据this,(直接由BoD复制粘贴):

  • 您不得使用android:thumbandroid:track,而应使用app:thumbapp:track 并且您必须将以下内容添加到您的xml 文档的根目录中:

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

【讨论】:

  • 太棒了。非常感谢!
  • 我在 thumb 和 track 之前尝试了应用前缀,但它给出了错误,但它仍然不支持 thumbtint 和 tracktint。你能帮忙吗?谢谢
【解决方案2】:

Android Lollipop 及以上版本中最简单的方法,

<style name="AppTheme" parent="MaterialTheme.Light">
    ...
    <item name="android:colorControlActivated">@color/color_switch</item>
</style>

【讨论】:

    【解决方案3】:

    试试这个:

    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"
    

    【讨论】:

      【解决方案4】:

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

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

      现在把这个文件放到drawable文件夹中

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

      【讨论】:

      • 与问题无关,我不会因为我做不到而给负分
      • 嗯,它回答了标题中假设的问题。
      【解决方案5】:

      可能是你需要放

      style="@style/switchStyle"
      

      在您的 XML 中也是如此

      【讨论】:

        【解决方案6】:

        供 androidx.swithcomapact 使用

                           app:thumbTint="@color/purple_700"
                            app:trackTint="@color/purple_700"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-11
          • 1970-01-01
          相关资源
          最近更新 更多