What is a switch? Let's take a look at the switch implementation effect
It's like a button that turns on mobile data on your phone.
Property:
- Android:showtext: Sets whether to display text when on/off, Boolean
- Android:splittrack: whether to set a gap, let the slider and the bottom picture separated, Boolean
- android:switchminwidth: Set the minimum width of the switch
- android:switchpadding: Sets the spacing of text within the slider
- android:switchtextappearance: Set the text appearance of the switch, temporarily did not find what use ...
- Android:textoff: text displayed when the button is not selected
- Android:texton: text displayed when the button is selected
- Android:textstyle: text style, bold, italic underline those
- android:track: picture at the bottom
- android:thumb: Picture of the slider
- android:typeface: setting fonts
It is easier to use this control, as long as it is referenced in the layout file and then set the associated property.
<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:app="Http://schemas.android.com/apk/res-auto"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:gravity="Center"Tools:context="Com.contentprovide.liuliu.togglebutton_test. Mainactivity"> <Switch Android:id="@+id/swh"Android:layout_width="wrap_content"Android:layout_height="wrap_content" android:track="@drawable/tra" android:thumb=" @drawable/a3" Android:textoff="off"Android:texton=" on"/></linearlayout>
My track and thumb reference two drawable resource files are defined by myself
Tra.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/a2"></item><item android:state_checked="false"android:drawable="@drawable/a1"></item></selector>
A1.xml, A2.xml, A3.xml are custom graphics, relatively simple, here is not recorded
Android Switch: Switch control