Android basic getting started -- 2.3.6 ToggleButton and Switch, 2.3.6togglebutton
Android basic getting started -- 2.3.6 ToggleButton and Switch
Tags (separated by spaces): basic Android tutorial
This section introduces:
This section describes the basic Android UI controls: ToggleButton and Switch.
I am not familiar with it. I suddenly think of my first outsourcing company, whether it is a network-connected Switch Under wifi, The TextView actually used, and then called the artist.
And two diagrams before and after switching, and then set them in the Code. Of course, when you click TextView, you can determine the status and set the corresponding background...
Okay, too. Okay... In this section, both are actually switch components, but the latter must be used after Android 4.0.
Therefore, the minsdk in the AndroidManifest. xml file must be greater than or equal to 14. Otherwise, an error is returned ~, Let's take a look at what the two controls look like first,
The last two widgets of Android 5.0 are much better than the previous ones. Let's take a look at the first two widgets:
ToggleButton and Switch before 5.0:
Version 5.0:
Well, a clear comparison... Next, let's take a look at the use of the two controls. In fact, the use of the two controls is almost the same.
Paste the official API first: Switch; ToggleButton
1. Explanation of core attributes: 1) ToggleButton (switch button)
The following attributes can be set:
- Android: disabledAlpha: Sets the transparency of the button when it is disabled.
- Android: textOff:Text displayed when the button is not selected
- Android: textOn:Text displayed when the button is selected
In addition, we can also write a selector and set the Background attribute ~
2) Switch)
The following attributes can be set:
- Android: showText:Whether to display text when setting on/off, boolean
- Android: splitTrack:Whether to set a gap to separate the slider from the bottom image, boolean
- Android: switchMinWidth:Set the minimum width of the switch.
- Android: switchPadding:Set the text interval in the slider
- Android: switchTextAppearance:Set the text appearance of the switch. Nothing is available for the moment...
- 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-style dashes
- Android: track:Bottom Image
- Android: thumb:Slider Image
- Android: typeface:Set the font. Three types of fonts are supported by default: sans, serif, and monospace.
Other font files (*. Ttf), First save the font file in the assets/fonts/directory, but you need to set it in Java code:
** Typeface typeFace = Typeface. createFromAsset (getAssets (), "fonts/HandmadeTypewriter. ttf ");
TextView. setTypeface (typeFace );**
2. Example:
Because it is relatively simple, we write them together. In addition, we set the slider and the image at the bottom for the Switch to implement
A slider similar to IOS 7, but one drawback is that you cannot set the size of the slider and the bottom in XML,
That is, the size of the material and the size of the Switch. We can get the Drawable object in Java and modify the size,
Simple Example:
Run:
Implementation Code:
First, two drawable files:
Thumb_selctor.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/switch_btn_pressed"/> <item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/></selector>
Track_selctor.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/switch_btn_bg_green"/> <item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/></selector>
Layout file: activity_main.xml:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <ToggleButton android: id =" @ + id/tbtn_open "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: checked =" true "android: textOff = "Disable sound" android: textOn = "enable sound"/> <Switch android: id = "@ + id/swh_status" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textOff = "" android: textOn = "" android: thumb = "@ drawable/thumb_selctor" android: track = "@ drawable/track_selctor"/> </LinearLayout>
MainActivity. java:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = ". mainActivity "> <ToggleButton android: id =" @ + id/tbtn_open "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: checked =" true "android: textOff = "Disable sound" android: textOn = "enable sound"/> <Switch android: id = "@ + id/swh_status" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textOff = "" android: textOn = "" android: thumb = "@ drawable/thumb_selctor" android: track = "@ drawable/track_selctor"/> </LinearLayout>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.