Order: The switch button control is used in today's project, and some information has been specially written in this blog record.
1.Switch
<Switch android:id= "@+id/bt" android:layout_width= "Wrap_content" android: Layout_height= "Wrap_content" Android:texton= "Turn on" android:layout_marginleft= " 20DP " android:textoff=" Off " android:showtext=" true " android:thumb = "@drawable/shape_thum" android:track= "@drawable/select_bg_switch" />
Here Layout_width: This can set the width of the entire layout, cannot set the specific switch size, you need to use the Switchminwidth property to set.
Thumb: The background that the text is carrying, set as the background color to hide. No setting will appear with a background box.
Track: Sets the background image of the switch, similar to the button's background.
Textoff, Texton: Text display when setting the switch.
2.ToggleButton
<ToggleButton android:layout_width= "80DP" android:layout_height= "20DP" Android:id= "@+id/toggle" android:layout_centerhorizontal= "true" Android:background = "@drawable/shape_track_on" android:textoff= "Off" android:texton= "on" android:layout_marginleft= "20DP" />
3.
1. Layout
Activity_main.xml
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center"android:orientation= "Horizontal"Android:layout_margintop= "30DP" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Switch"/> <ToggleButton android:layout_width= "80DP"Android:layout_height= "20DP"Android:id= "@+id/toggle"Android:layout_centerhorizontal= "true"Android:background= "@drawable/shape_track_on"Android:textoff= "Off"Android:texton= "On"Android:layout_marginleft= "20DP"/> </LinearLayout> <LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"android:layout_centerinparent= "true" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "On State"/> <Switch Android:id= "@+id/bt"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:texton= "Open"Android:layout_marginleft= "20DP"Android:textoff= "Off"Android:showtext= "true"Android:thumb= "@drawable/shape_thum"Android:track= "@drawable/select_bg_switch"/> </LinearLayout></RelativeLayout>
Shape_thum.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Oval" > <solid android:color= "#0f0"/> <size android:height= "30DP" android:width = "30DP"/></shape>
Shape_track_off.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Rectangle" > <corners android:radius= "30DP"/> <solid android:color= "@android: Color/darker_gray "/></shape>
Shape_track_on,xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" Android:shape= "Rectangle" > <corners android:radius= "30DP"/> <solid android:color= "@android: Color/holo_red_light "/></shape>
Select_bg_switch.xml
<?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/shape_track_on "/> < Item android:state_checked= "false" android:drawable= "@drawable/shape_track_off"/></selector>
Mainactivity
Public classMainactivityextendsappcompatactivity {PrivateSwitch Aswitch; PrivateToggleButton Toggle; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Aswitch=Findviewbyid (R.ID.BT); Toggle=Findviewbyid (R.id.toggle); //State Change MonitoringAswitch.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { if(isChecked) {toast.maketext (mainactivity. This, "Open", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Close", Toast.length_short). Show (); } } }); //State Change MonitoringToggle.setoncheckedchangelistener (NewCompoundbutton.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { if(isChecked) {toast.maketext (mainactivity. This, "Open", Toast.length_short). Show (); }Else{toast.maketext (mainactivity). This, "Close", Toast.length_short). Show (); } } }); }}
Android switching control switch and ToggleButton