android 开发之切换landscape屏幕

在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> 

 

 

 

转载于:https://www.cnblogs.com/sun_catboy/archive/2010/04/23/1718967.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值