2017-03-01 44 views

回答

0

這裏是很好的例子:

ORIGINAL ANSWER

您可以定義用作背景的圖形內容和這樣的切換器部分:

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

現在,您需要創建一個選擇器,用於定義可繪製切換器的不同狀態。 這裏從Android源的副本:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" android:drawable="@drawable/switch_thumb_disabled_holo_light" /> 
    <item android:state_pressed="true" android:drawable="@drawable/switch_thumb_pressed_holo_light" /> 
    <item android:state_checked="true" android:drawable="@drawable/switch_thumb_activated_holo_light" /> 
    <item        android:drawable="@drawable/switch_thumb_holo_light" /> 
</selector> 

這定義了拇指可繪製,即上方移動背景的圖像。有用於滑塊4個ninepatch圖像:

失活的版本(即Android是使用xhdpi版本) The deactivated version
壓制滑塊: The pressed slider
活化的滑塊(接通狀態): The activated slider
的默認版本(關閉狀態): enter image description here

在以下選擇器中定義的背景還有三種不同的狀態:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_enabled="false" android:drawable="@drawable/switch_bg_disabled_holo_dark" /> 
    <item android:state_focused="true" android:drawable="@drawable/switch_bg_focused_holo_dark" /> 
    <item        android:drawable="@drawable/switch_bg_holo_dark" /> 
</selector> 

停用的版本: The deactivated version
聚焦的版本: The focused version
和默認版本: the default version

爲了有一個風格的切換隻需創建此兩個選擇,它們設置爲你的交換機查看,然後將這七個圖像更改爲所需的樣式。

+0

謝謝你的回答!但這個控件應該像SwitchCompat(不像單選按鈕) - 當用戶從左向右拉時,應該改變狀態,反之亦然 –

+0

@ElviraIatsko我編輯了我的answear –

相關問題