Status Switch and togglebuttonswitch
XML attributes supported by ToggleButton and related methods
1. android: checked -----> setChecked (boolean)
-----> Set whether the button is selected
2. android: textOff -----> set the text displayed when the button is disabled
3. android: textON -----> set the text displayed when the button is enabled
XML attributes supported by Switch and related methods
1. android: checked -----> setChecked (boolean)
-----> Set whether the button is selected
2. android: textOff -----> set the text displayed when the button is disabled
3. android: textON -----> set the text displayed when the button is enabled
4. android: switchMinWidth -----> setSwitchMinWidth (int)
-----> Set the minimum width of the switch
5. android: switchPadding -----> setSwitchMinWidth (int)
-----> Set the gap between the switch and the title text
6. android: switchTextAppearance -----> setSwitchTextAppearance (Context, int)
-----> Set the text style on the switch icon
7. android: textStyle -----> setSwitchTypeface (TYpeface)
-----> Set the text style of the switch.
8. android: thumb -----> setThumbResource (int)
-----> Specify the switch button for drawing the switch using the custom Drawable
9. android: track -----> setTrackResource (int)
-----> Specify to use custom Drawable to draw the track of the switch
10. android: typeface -----> setSwitchTypeface (TYpeface)
----- Set the font style of the switch text
Demo2 \ togglebutton_demo \ src \ main \ res \ layout \ activity_main.xml
1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical" 6 tools: context = ". mainActivity "> 7 8 <! -- Define a ToggleButton --> 9 <ToggleButton10 android: id = "@ + id/toggle" 11 android: layout_width = "wrap_content" 12 android: layout_height = "wrap_content" 13 android: checked = "true" 14 android: textOff = "horizontal arrangement" 15 android: textOn = "Vertical arrangement"/> 16 <! -- Define a linear layout that can dynamically change the direction --> 17 <LinearLayout18 android: id = "@ + id/test" 19 android: layout_width = "match_parent" 20 android: layout_height = "match_parent" 21 android: orientation = "vertical"> 22 23 <Button24 android: layout_width = "wrap_content" 25 android: layout_height = "wrap_content" 26 android: text = "first button"/> 27 <Button28 android: layout_width = "wrap_content" 29 android: layout_height = "wrap_content" 30 android: text = "second button"/> 31 <Button32 android: layout_width = "wrap_content" 33 android: layout_height = "wrap_content" 34 android: text = "third button"/> 35 </LinearLayout> 36 37 </LinearLayout>
Demo2 \ togglebutton_demo \ src \ main \ java \ com \ ly \ togglebutton_demo \ MainActivity. java
1 import android. app. activity; 2 import android. OS. bundle; 3 import android. widget. compoundButton; 4 import android. widget. linearLayout; 5 import android. widget. toggleButton; 6 7 public class MainActivity extends Activity {8 private ToggleButton toggle; 9 private LinearLayout linear; 10 @ Override11 protected void onCreate (Bundle savedInstanceState) {12 super. onCreate (savedInstanceState); 13 setContentView (R. layout. activity_main); 14 toggle = (ToggleButton) findViewById (R. id. toggle); 15 linear = (LinearLayout) findViewById (R. id. test); 16 toggle. setOnCheckedChangeListener (new CompoundButton. onCheckedChangeListener () {17 @ Override18 public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {19 if (isChecked) {20 // if selected, set the vertical layout 21 linear. setOrientation (LinearLayout. VERTICAL); 22} else {23 // otherwise, the level is 24 linear. setOrientation (LinearLayout. HORIZONTAL); 25} 26} 27}); 28} 29 30 31}