2015-03-03 59 views
0

我有兩個圖形:的樣式開關如何設置正確的圖像

enter image description here

和:

enter image description here

第一圖形應該是開關的那種背景,但是當我設定它:

android:background="@drawable/custom_background" 

Th e圖像未設置。根本沒有背景。 我可以成功設置第二圖形使用:

android:thumb="@drawable/custom_switch_thumb" 

如何設置開關的背景是什麼?

回答

1

使用android:track="@drawable/custom_background"

如果要添加顏色自定義開關,您還可以使用的trackthumb選擇,然後用你的彩色圖像中選擇。這將允許你這樣做提供電流的開關狀態的一些視覺反饋給用戶

更新

在你繪製文件夾中創建一個名爲switch_bg.xml與文件,內容如下。 switch_bg_onswitch_bg_off是用於軌道的圖像。利用你想要的顏色來打開和關閉狀態。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:drawable="@drawable/switch_bg_on" /> 
    <item        android:drawable="@drawable/switch_bg_off" /> 
</selector> 

然後在可繪製文件夾中創建一個文件switch_thumb.xmlswitch_thumb_onswitch_thumb_off再次是圖像。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_checked="true" android:drawable="@drawable/switch_thumb_on" /> 
    <item        android:drawable="@drawable/switch_thumb_off" /> 
</selector> 

然後在你的交換機視圖使用類似如下:

<Switch 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:track="@drawable/switch_bg" 
    android:thumb="@drawable/switch_thumb" /> 
+0

確定設置我的第一個圖標賽道工作正常。你提到我在設置自定義曲目時仍然可以使用着色?你有一個例子嗎? – Mulgard 2015-03-03 11:18:25

+0

請看看更新 – peshkira 2015-03-03 11:22:43

相關問題