本文实例讲述了android改变手机屏幕朝向的方法。分享给大家供大家参考。具体如下:
模拟当点击按钮时,使手机朝向发生改变。
main.xml布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?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>
|
清单文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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);
}
}
|
运行结果:
希望本文所述对大家的android程序设计有所帮助。