2016-11-28 42 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屬性。