android动态更改屏幕方向

在androd中要通过程序改变屏幕显示的方向,必须覆盖setRequestOrientation()方法,若要取得目前屏幕方向则需要用到getRequestOrientation()方法。
本例子实现了简单的改变屏幕方向,通过一个按钮点击事件,判断当前的屏幕方向,例如竖排(PORTRAIT),则改为横排(LANDSCAPE);
本程序重写setRequestOrientation()方法,起目的是为了要捕捉设置屏幕放向的同时所触发的事件,并在更改的时候以Toast显示要更改的方向。
程序一开始闲判断getRequestOrentation()的值是否为-1,表示在Activity属性里没有设置android:screenOrientation="portrait",这意味着即使点击按钮也无法判断屏幕的方向,不会进行更改方向的事件了。

布局文件main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/textView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    />
<Button 
	android:id="@+id/button01"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:gravity="center_horizontal"
	android:text="@string/button_text01"/>
</LinearLayout>

 OrientationActivity.java

public class OrientationActivity extends Activity {
	private TextView tv;
	private Button button;
	
	private Resources rs;
	private Display display;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        display = getWindow().getWindowManager().getDefaultDisplay();
        tv = (TextView) this.findViewById(R.id.textView01);
        button = (Button) this.findViewById(R.id.button01);
        rs = this.getResources();
        if(getRequestedOrientation() == -1){
        	tv.setText(rs.getText(R.string.err_msg));
        }
        
        button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				/* 方法一 */
				if(getRequestedOrientation() == -1){
					tv.setText(rs.getText(R.string.err_msg));
				}else{
					/* 方法一 */
//					if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
//						/* 若当前为横排则改为竖排 */
//						setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//					}else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
//						/* 若当前为竖排则改为横排 */
//						setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//					}
					int h = display.getHeight();
					int w = display.getWidth();
					if(w < h){
						setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
					}else{
						setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
					}
				}
			}
        	
        });
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#setRequestedOrientation(int)
     */
    @Override
    public void setRequestedOrientation(int requestedOrientation) {
    	switch (requestedOrientation) {
		case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
			MakeTextToast.showToast(OrientationActivity.this,rs.getText(R.string.msg_2).toString() , true);
			break;
		case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
			MakeTextToast.showToast(OrientationActivity.this,rs.getText(R.string.msg_2).toString() , true);
			break;
		default:
			break;
		}
    	super.setRequestedOrientation(requestedOrientation);
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#getRequestedOrientation()
     */
    @Override
    public int getRequestedOrientation() {
    	// TODO Auto-generated method stub
    	return super.getRequestedOrientation();
    }
}

 MakeTextToast.java

public class MakeTextToast {
	/**
	 * 显示Toast
	 * @param context
	 * @param msg 信息
	 * @param isLong 显示时间长短
	 */
	public static void showToast(Context context,String msg,boolean isLong){
		if(isLong){
			Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		}
	}
}

 

Manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OrientationActivity"
                  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>

 运行结果图:

竖排:

 

横排:
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值