The Open source project introduced today is excellent, but also the work of the people. I've been in touch with a lot of custom switch before, some animations are stiff, some can't customize the width of the switch, some can only define the width of the slider cannot be set. However, this project offers a variety of custom options that are perfect!
Project address:Https://github.com/kyleduo/SwitchButton
Author Blog:http://www.kyleduo.com/
One, using controls in XML
After downloading and importing Lib, let's put this control in the XML.
< android:id = "@+id/sb_default" style = "@style/ Switchbuttonstyle " Android:layout_width = "200DP" Android:layout_height = "100DP" App:thumb_width = "90DP" App:thumb_height = "90DP" />
Don't forget to add a namespace:xmlns:app= "Http://schemas.android.com/apk/res-auto"
It is commendable that this control provides a perfect real-time preview in Eclipse preview:
Ii. various attributes in XML
In an XML layout file, you can configure the face of the switch button using these attrs.
- ondrawable: drawable of background for status on
- offdrawable: drawable of background for status OFF
- thumbdrawable: drawable of thumb
- thumb_margin: Set inner margin between thumb and edges
- thumb_marginleft/top/bottom/right: Set margin for specific edge
- thumb_width: Set the width of thumb, probably used for gradient drawable
- thumb_height: Set the height of the thumb
- onColor: Set the color of status on, USD for flat version, and the priority is below of ondrawable
- Offcolor: Like the OnColor
- Thumbcolor: Like the OnColor
- Thumbpressedcolor: Like the Thumbcolor, but for pressed status
- animationvelocity: distance of animation per frame
- radius: Used for color version, radius of corner of background and thumb.
- measurefactor: Factor limit the minimum width equals almost (the height of the thumb * measurefactor)
If you want to define a style, you can write it in the style file.
<stylename= "Materialdesignstyle"Parent= "MD"> <Itemname= "Android:layout_width">Wrap_content</Item> <Itemname= "Android:layout_height">Wrap_content</Item> <Itemname= "Android:padding">16dp</Item> </style>
Everything is convenient. We can also refer to the demo, customize the slider and background, and define the switch's slider bar. Please see the demo for details.
Iii. operations in Java
3.1 Listeners
// listener to listen for events Sbdefault.setoncheckedchangelistener (new Oncheckedchangelistener () { @Override publicvoidboolean isChecked) { toast.maketext (styleactivity. This, "Default style button, new state:" + (isChecked? ") On ":" Off "), Toast.length_short). Show (); } });
3.2 Setting Styles
Configuration conf = Configuration.getdefault (getresources (). Getdisplaymetrics (). density); Conf.setthumbmargin (// Set the border of the small slider // Set the Click Slider to slide the on/off rate // width of The small slider / / set Edge radian / / limit minimum width // Apply the above settings to switch
3.3 Setting the status
Sb.setenabled (isChecked);
Animated state toggle with slider animation When switch changes state after setting
// Slide Toggle Switch Effect
3.4 Flipping the current state
With toggle (), a sliding animation will appear when switching.
//
With toggle (false), the animation is not displayed when switching
Sb.toggle (false);
SOURCE Download:http://download.csdn.net/detail/shark0017/8372607
Use open source project Switchbutton to implement various styles of switch