Java 在 Android 中更改 Switch 的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21734371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:24:58  来源:igfitidea点击:

Change color of Switch in Android

javaandroidxmlandroid-themeandroid-styles

提问by EGHDK

I'm trying to change the color of my switch in Android. I realize that I will need new 9patches. I went over to http://android-holo-colors.com/and selected my color and selected (Switch Jelly Bean). To use Switch Jelly Bean I had to use: https://github.com/BoD/android-switch-backport. To import it into my project I had to add:

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

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

to my styles, and then in xml I have to use the switch like so:

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

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

Now everything with the switch works fine. Next, I took everything that was output from the android holo color generator and put it into the proper files:

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

  • drawable (2 selector files)
  • drawable-hdpi (9patch files)
  • drawable-xhdpi (9patch files)
  • drawable-xxhdpi (9patch files)
  • drawable(2 个选择器文件)
  • drawable-hdpi(9patch 文件)
  • drawable-xhdpi(9 个补丁文件)
  • drawable-xxhdpi(9patch 文件)

then I added to my xml:

然后我添加到我的 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" />

but it is still the original blue color. I believe I'm doing everything correctly. Everything compiles (xml, java). Note: I AM importing org.jraf.android.backport.switchwidget.Switchin my java also. Any ideas?

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

采纳答案by velis

As per this, (direct answer by BoD copy-paste):

按照,(由董事会复制-粘贴直接回答):

  • You must not use android:thumband android:track, but instead, app:thumband app:trackAnd you must add the following to the root of your xml document:

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

  • 不得使用android:thumbandroid:track,而是app:thumbapp:track你必须添加以下到您的XML文档的根:

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

回答by SDJMcHattie

It might be that you need to put

可能是你需要把

style="@style/switchStyle"

in your XML as well

在您的 XML 中

回答by ROHIT PARMAR

to change the color of switch you can use two images

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

  1. green - when switch is ON
  2. red - when switch is OFF
  1. 绿色 - 当开关打开时
  2. 红色 - 当开关关闭时

now put this file in drawable folder

现在把这个文件放在 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>

and in the layout XML file use it as below

并在布局 XML 文件中使用它,如下所示

<CheckBox
    android:id="@+id/user_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/notif_checkbox_selector"
/>

回答by sb_269

Try this:

尝试这个:

switch_thumb.xml

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>

In the layout of the switch:

在开关的布局中:

android:thumb="@drawable/switch_thumb"

回答by emen

Easiest way in Android Lollipop and above,

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

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