In the application we often see some of the selection switch status of the configuration file, when the project is using the Android switch control, but feel good ugly look ...
Personally think it is better to customize, first of all:
Implementation process:
1. Prepare two images for the switch in different states into the drawable.
Add code to the 2.xml file:
<togglebutton android:id= "@+id/switch1" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:layout_gravity= "center" android:background= "@android: Color/transparent" android:button= "@drawable/btn_backgrounds" android:checked= "false" android:textoff= "" Android:texton= ""/>
android:button= "@drawable/btn_backgrounds" is your own definition of Android selector file:
<selector xmlns:android= "http://schemas.android.com/apk/res/android" > <item android:state_ Checked= "true" android:drawable= "@drawable/toggle_btn_on"/> <item android:drawable= "@drawable/toggle_ Btn_off "/>
3, then declare the ToggleButton control in the Java file and instantiate it, adding the Click event:
/*** Self out created inner class, listen button click event * *@authorCYF **/ classMyoncheckedchangelistenerImplementsOncheckedchangelistener {@Override Public voidOnCheckedChanged (Compoundbutton arg0,Booleanarg1) { if(arg1) { // open } Else { //off } } }
4. Add Monitoring:
New Myoncheckedchangelistener ());
Note Memo, this does not have a switch sliding effect ~ ~ ~
android-Custom Switch Button implementation