码迷,mamicode.com
首页 > 移动开发 > 详细

四、 Android之手机屏幕朝向

时间:2014-05-28 02:11:11      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:android   c   class   blog   code   java   

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

main.xml布局文件

bubuko.com,布布扣
bubuko.com,布布扣
<?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>
bubuko.com,布布扣
bubuko.com,布布扣

清单文件

bubuko.com,布布扣
bubuko.com,布布扣
<?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>
bubuko.com,布布扣
bubuko.com,布布扣

OrientationActivity类

bubuko.com,布布扣
bubuko.com,布布扣
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;

publicclass OrientationActivity extends Activity {
private EditText editText=null;

@Override
publicvoid 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(){
publicvoid 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);
}
elseif(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
OrientationActivity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

}

}
});
}

/**
* 配置信息发生改变时触发
*/
@Override
publicvoid 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);

}
}
bubuko.com,布布扣
bubuko.com,布布扣

运行结果

bubuko.com,布布扣

四、 Android之手机屏幕朝向,布布扣,bubuko.com

四、 Android之手机屏幕朝向

标签:android   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/liyuzhao/p/3753742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!