我正在尝试更改 Android 中开关的颜色。我意识到我需要新的 9patches。我转到 http://android-holo-colors.com/ 并选择了我的颜色并选择了(Switch Jelly Bean)。要使用 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
。有任何想法吗?
原文由 EGHDK 发布,翻译遵循 CC BY-SA 4.0 许可协议
android:thumb
andandroid:track
, but instead,app:thumb
andapp:track
And you must add the following to the root of your xml文档:xmlns:app="http://schemas.android.com/apk/res-auto"