简体   繁体   中英

Android change switch image

I would like to know how to change the image of a switch in android. I know that we can customize the switch and change the color etc. but how do we use another image for the switch. I tried setting the background from my drawables but the switch is still visible and appears on top of my image. I understand that we can use toggle button, or image button however I am curious if we can do so to a switch.

Override android:thumb and android:track in Switch component to override switch image and background respectiveli. For eg:

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/drawable_switch"
android:track="@drawable/drawable_bg" />

where drawable_switch and drawable_bg are selectors respectively.

drawable_switch.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_disabled" />
<item android:state_pressed="true"  android:drawable="@drawable/switch_pressed" />
<item android:state_checked="true"  android:drawable="@drawable/switch_enabled" />
<item android:drawable="@drawable/switch_default" />

drawable_bg.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled" />
<item android:state_focused="true"  android:drawable="@drawable/switch_bg_focused" />
<item android:drawable="@drawable/switch_bg_default" />

@Jerry try this and revert if you still have any issues

android:thumb属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM