在android开发中,我们的程序可能需要切换屏幕来适应应用对屏幕的要求,比如在电影播放的时候,横屏就比竖屏有更好的用户体验,
下面我简单介绍一下landscape的切换.其实方法很简单就是重载几个函数就可以了.
--------------Landscape.java-----------------
import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Landscape extends Activity { @Override public int getRequestedOrientation() { return super.getRequestedOrientation(); } @Override public void setRequestedOrientation(int requestedOrientation) { switch (requestedOrientation) { case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE): mMakeTextToast(getResources().getText(R.string.hello).toString(), false); break; case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT): mMakeTextToast(getResources().getText(R.string.hello).toString(), false); break; } super.setRequestedOrientation(requestedOrientation); } private void mMakeTextToast(String string, boolean b) { if (b) { Toast.makeText(Landscape.this, string, Toast.LENGTH_LONG).show(); } else { Toast.makeText(Landscape.this, string, Toast.LENGTH_SHORT).show(); } } private Button btn; private TextView tv; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.Button01); tv = (TextView) findViewById(R.id.TextView01); if (getRequestedOrientation() == -1) { tv.setText(getResources().getText(R.string.str_cannotrot)); } btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (getRequestedOrientation() == -1) { tv.setText(getResources().getText(R.string.str_cannotrot)); } else { if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } } }); } }
-------------main.xml---------------
<?xml version="1.0" encoding="utf-8"?> <merge android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TableLayout android:id="@+id/TableLayout01" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </TableRow> <TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_height="wrap_content"> <Button android:text="@string/str_landscape" android:id="@+id/Button01" android:gravity="center_horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </TableRow> </TableLayout> </merge>
------------------Manifest.xml-------------------
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.landscape" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Landscape" android:label="@string/app_name" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>