<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_sreen" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cheng.myapplication.ScreenActivity"> <Switch android:id="@+id/id_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/q" android:track="@drawable/p" android:minWidth="50dp" android:showText="true" android:textOn="竖屏" android:textOff="横屏" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="现在是竖屏"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_sreen" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cheng.myapplication.ScreenActivity"> <Switch android:id="@+id/id_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:thumb="@drawable/q" android:track="@drawable/p" android:minWidth="50dp" android:showText="true" android:textOn="竖屏" android:textOff="横屏" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="现在是横屏"/> </LinearLayout>
package com.example.cheng.myapplication; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; public class ScreenActivity extends Activity { Switch mSwitch; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sreen); mSwitch=(Switch)findViewById(R.id.id_switch); textView=(TextView)findViewById(R.id.textView); final LinearLayout layout=(LinearLayout)findViewById(R.id.activity_sreen); mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ layout.setOrientation(LinearLayout.VERTICAL); }else{ layout.setOrientation(LinearLayout.HORIZONTAL); } } }); } }
Powered by codetiler.com