2016-11-28 44 views
1

我想知道如何更改android中的开关图像。我知道我们可以定制开关并更改颜色等,但我们如何使用另一个图像作为开关。我尝试从我的绘图设置背景,但开关仍然可见并出现在我的图像上。我明白,我们可以使用切换按钮或图像按钮,但我很好奇,如果我们可以这样做到开关。Android更改开关图像

+0

可能duplica te [如何自定义切换按钮?](http://stackoverflow.com/questions/23358822/how-to-custom-switch-button) – Beloo

+0

试试这个[回复](http://stackoverflow.com/questions/ 28981164/android-switch-change-background-of-switch-on-off/28981942#28981942) –

回答

1

覆盖Switch组件中的android:thumb和android:track以覆盖切换图像和背景色。 对于例如:

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

其中drawable_switch和drawable_bg分别是选择器。

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试试这个和恢复,如果你还有其他问题

+0

谢谢!这对我有用。 – JerryKo

0

还有android:thumb属性。