Mainactivity.java
1 publicclassmainactivityextendsactivity{2 @Override3 protectedvoid onCreate (Bundle savedinstancestate) {4 super.oncreate (savedinstancestate);5 Setcontentview (r.layout.activity_main);6ToggleButton ToggleButton =(ToggleButton) Findviewbyid (R.ID.TOG_BTN);7Finallinearlayout layout =(LinearLayout) Findviewbyid (r.id.text);8 Togglebutton.setoncheckedchangelistener (Newoncheckedchangelistener () {9 @OverrideTen publicvoid oncheckedchanged (Compoundbutton Buttonview,Boolean isChecked) { A //TODO auto-generated Method Stub - if(isChecked) { -Layout.setorientation (1); the}Else{ -Layout.setorientation (0); - } - } + }); - } +}
Layout FileActivity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/text0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ToggleButton
android:id="@+id/tog_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:textOff="横向"
android:textOn="纵向"/>
<LinearLayout
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
ToggleButton, Status Select button, can display the layout dynamically