Android之手机屏幕朝向

模拟当点击按钮时,使手机朝向发生改变。

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical" android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<Button android:id="@+id/btn"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="点击更改屏幕朝向" />
<!-- android:hint: 当文本为空时显示该文本 -->
<EditText android:id="@+id/editText"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:cursorVisible
="false"
android:hint
="显示当前屏幕朝向" />
</LinearLayout>

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity"
android:versionCode
="1"
android:versionName
="1.0">
<!-- 设置手机的朝向,不然无法获取手机的朝向
android:screenOrientation
="portrait"
android:configChanges
="orientation" -->
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".OrientationActivity"
android:label
="@string/app_name"
android:screenOrientation
="portrait"
android:configChanges
="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="7" />
<!-- 改变手机配置权限 -->
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
</manifest>

OrientationActivity类

package com.ljq.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OrientationActivity extends Activity {
private EditText editText=null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

editText
=(EditText)findViewById(R.id.editText);
Button btn
=(Button)findViewById(R.id.btn);
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v) {
//判断是否可以获得requestedOrientation属性
if(OrientationActivity.this.getRequestedOrientation()==-1){
Toast.makeText(OrientationActivity.
this, "系统的朝向无法获取", Toast.LENGTH_LONG).show();
}
else{
//手机屏幕的朝向有7个可选值,分别如下
//SCREEN_ORIENTATION_BEHIND: 继承Activity堆栈中当前Activity下面的那个Activity的方向
//SCREEN_ORIENTATION_LANDSCAPE: 横屏(风景照) ,显示时宽度大于高度
//SCREEN_ORIENTATION_PORTRAIT: 竖屏 (肖像照) , 显示时高度大于宽度
//SCREEN_ORIENTATION_NOSENSOR: 忽略物理感应器——即显示方向与物理感应器无关,
//不管用户如何旋转设备显示方向都不会随着改变("unspecified"设置除外)
//SCREEN_ORIENTATION_SENSOR: 由物理感应器决定显示方向,它取决于用户如何持有设备,
//当设备被旋转时方向会随之变化——在横屏与竖屏之间
//SCREEN_ORIENTATION_UNSPECIFIED: 未指定,此为默认值,由Android系统自己选择适当的方向,
//选择策略视具体设备的配置情况而定,因此不同的设备会有不同的方向选择
//SCREEN_ORIENTATION_USER: 用户当前的首选方向
if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
OrientationActivity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
OrientationActivity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

}

}
});
}

/**
* 配置信息发生改变时触发
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Toast.makeText(
this, "系统的屏幕方向发生改变", Toast.LENGTH_LONG).show();
int o=getRequestedOrientation();//获取手机的朝向
switch (o) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
editText.setText(
"当前屏幕朝向为: 横屏");
break;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
editText.setText(
"当前屏幕朝向为: 竖屏");
break;
}
//不能省略,否则会报android.app.SuperNotCalledException: Activity OrientationActivity did not
//call through to super.onConfigurationChanged()异常
super.onConfigurationChanged(newConfig);

}
}

运行结果

posted on 2011-02-25 10:36  Ruthless  阅读(17962)  评论(0编辑  收藏  举报