Android学习第十三天----ToggleButton
xml中
<RelativeLayout 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" tools:context=".MainActivity" > <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/togglebutton1" android:textOn="纵向排列" android:textOff="横向排列" android:checked="true"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/togglebutton1" android:layout_marginTop="24dp" android:orientation="vertical" android:id="@+id/linerlayoutid"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" /> </LinearLayout> </RelativeLayout>
java代码
package cn.will.test; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.LinearLayout; import android.widget.ToggleButton; public class MainActivity extends Activity { private ToggleButton mToggleButton = null; private LinearLayout mLinearLayout = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLinearLayout = (LinearLayout) findViewById(R.id.linerlayoutid); mToggleButton = (ToggleButton) findViewById(R.id.togglebutton1); mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { if (isChecked) { mLinearLayout.setOrientation(LinearLayout.VERTICAL); } else { mLinearLayout.setOrientation(LinearLayout.HORIZONTAL); } } }); } }