MainActivity.java
1 publicclassMainActivityextendsActivity{ 2 @Override 3 protectedvoid onCreate(Bundle savedInstanceState){ 4 super.onCreate(savedInstanceState); 5 setContentView(R.layout.activity_main); 6 ToggleButton toggleButton =(ToggleButton)findViewById(R.id.tog_btn); 7 finalLinearLayout layout =(LinearLayout) findViewById(R.id.text); 8 toggleButton.setOnCheckedChangeListener(newOnCheckedChangeListener(){ 9 @Override 10 publicvoid onCheckedChanged(CompoundButton buttonView,boolean isChecked){ 12 // TODO Auto-generated method stub 13 if(isChecked){ 14 layout.setOrientation(1); 15 }else{ 16 layout.setOrientation(0); 17 } 18 } 19 }); 20 } 21 }
布局文件activity_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>
时间: 2024-10-12 04:22:35