demolayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<ToggleButton
android:id="@+id/power_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Power On"
androudLbackground="@drawable/togger"
android:textOff="Power Off" />
</LinearLayout>
建立一個togger.xml放置開與關的圖片(這個檔案放在drawable)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false"/>
</selector>
toggle_on.png
togger_off.png
參考資料:
https://github.com/kyleduo/SwitchButton
沒有留言:
張貼留言