2017-03-06 56 views
-1

我有一個關於Android開關按鈕的問題。有誰知道如何自定義它,以便看起來像:自定義開關按鈕Android

enter image description here

我已經找到了我如何可以替換使用Android的自定義圖像圈:拇指=,但仍然不知道如何增加的寬度整個按鈕使其填充大部分屏幕寬度,並在其上添加文本。還需要爲圖像添加一些偏移,就像在圖像上一樣。我可以通過使用Switch實現這一目標嗎?或者我必須自己實現整個目標?

+0

https://github.com/kyleduo/SwitchButton – Mrinmoy

+0

的[幻燈片切換爲Android](可能的複製http://stackoverflow.com/問題/ 9752760 /滑動切換換機器人) –

回答

1

使用以下實現

<Switch 
        android:id="@+id/th" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:thumb="@drawable/apptheme_switch_inner_holo_light" 
        app:track="@drawable/apptheme_switch_track_holo_light" 
        app:textOn="@string/switch_yes" 
        app:textOff="@string/switch_no" 
        android:textColor="#000000" 
        /> 

中創建一個名爲RES XML colors.xml /值文件夾:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="red">#ff0000</color> 
    <color name="green">#00ff00</color> 
</resources> 

在繪製文件夾中創建一個XML文件my_btn_toggle.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_checked="false" android:drawable="@color/red" /> 
     <item android:state_checked="true" android:drawable="@color/green" /> 
    </selector> 

和定義切換按鈕的xml部分添加:

android:background="@drawable/my_btn_toggle 

改變紋理基元和textOffuse的顏色

android:switchTextAppearance="@style/Switch"