Togglebutton is also a kind of button, which is reflected in two different States: Press and not press. This program uses togglebutton to dynamically change the layout of linearlayout.
Public class togglebuttontest extends activity {@ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); togglebutton toggle = (togglebutton) findviewbyid (R. id. toggle); Final linearlayout test = (linearlayout) findviewbyid (R. id. test); toggle. setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic void oncheckedchanged (compoundbutton arg0, Boolean arg1) {If (arg1) {// set the vertical layout of linearlayout test. setorientation (1);} else {// sets the horizontal layout of linearlayout test. setorientation (0 );}}});}}
Layout File
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <! -- Define a togglebutton --> <togglebutton Android: Id = "@ + ID/toggle" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textoff = "horizontal arrangement" Android: texton = "Vertical arrangement" Android: checked = "true"/> <! -- Define a linear layout that can dynamically change the direction --> <linearlayout Android: Id = "@ + ID/test" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <buttonandroid: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "test button 1"/> <buttonandroid: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "test button 2"/> <buttonandroid: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "test button 3"/> </linearlayout>