- 浏览: 393994 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
lulu15634219680:
Eclipse设置行宽Maximum line width -
gakes:
有没有正确的啊
Android手机号码判断正则表达式 -
GhostFromheaven:
mooshadow 写道你好,首先感谢你的程序。我最近也碰到了 ...
java合并两个XML文件 -
mooshadow:
你好,首先感谢你的程序。我最近也碰到了这个问题,用了你的程序之 ...
java合并两个XML文件 -
sunnyting:
蛮靠谱!解决问题哈!
Android控制屏幕方向的改变
目前大多数手机都支持 重力感应 ,随之而来的就是屏幕方向改变的问题。很多游戏都是仅横屏展示的,也有一些是仅竖屏展示的,更多的是横屏竖屏都可以的。
对应普通开发者来说,屏幕的随意改变也会带来困扰。在Google自带的doc里可以看到
如果设备的配置(在 Resources.Configuration 中进行了定义)发生改变,那么所有用户界面上的东西都需要进行更新,以适应新的配置。因为 Activity 是与用户交互的最主要的机制,它包含了处理配置改变的专门支持。除非你特殊指定,否则当配置发生改变(比如屏幕方向、语言、输入设备等等的改变)时你当前的 activity 都将被销毁,这销毁是通过一个正常的 activity 生命周期过程( onFreeze(Bundle) , onPause() , onStop() , 和 onDestroy() )进行的。如果 activity 之前正在前景画面,当这个实例的 onDestroy() 调用完成后将会启动这个 activity 的一个新的实例,并将前面那个实例中 onFreeze(Bundle) 所保存的内容传递给新的实例。因为任何的应用资源(包括 layout 文件)都有可能由于任何配置值而改变。因此处理配置改变的唯一安全的方法就是重新获取所有的资源,包括 layout 、绘图资源(原文 drawables )、字符串资源。由于 activity 已经如何保存自己的状态并从这些状态中重建自身,所以 activity 重新启动自身来获得新的配置将是一个非常便利的途径。在一些特殊的情况中,你可能希望当一种或者多种配置改变时避免重新启动你的 activity 。你可以通过在manifest中设置 android:configChanges 属性来实现这点。你可以在这里声明 activity 可以处理的任何配置改变,当这些配置改变时不会重新启动 activity ,而会调用 activity 的 onConfigurationChanged(Resources.Configuration) 方法。如果改变的配置中包含了你所无法处理的配置(在 android:configChanges 并未声明),你的 activity 仍然要被重新启动,而 onConfigurationChanged(Resources.Configuration) 将不会被调用。
在屏幕方向改变的时候,如果没有处理,程序会自动重启。对应一些需要保存用户数据的应用中,必须处理这种情况。
1>在AndroidManifest.xml中设置Activity的android:configChanges 属性。如:
<activity android:name=".AndroidLight"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这样就指定了屏幕方向改变和键盘隐藏时通知程序。
2>在程序中可以添加处理事件
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(TAG," == onConfigurationChanged"); processLayout();//自定义函数处理配置改变事件 }
3>也可以在AndroidManifest.xml中设置Activity的android:screenOrientation属性。如
<activity android:name=".AndroidLight" 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>
这样指定屏幕方向为竖屏。屏幕就不会自动旋转了。
4> android:configChanges 可选属性值
Constant |
Value |
Description |
mcc |
0x0001 |
The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code. |
mnc |
0x0002 |
The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code. |
locale |
0x0004 |
The locale has changed, that is the user has selected a new language that text should be displayed in. |
touchscreen |
0x0008 |
The touchscreen has changed. Should never normally happen. |
keyboard |
0x0010 |
The keyboard type has changed, for example the user has plugged in an external keyboard. |
keyboardHidden |
0x0020 |
The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation. |
navigation |
0x0040 |
The navigation type has changed. Should never normally happen. |
orientation |
0x0080 |
The screen orientation has changed, that is the user has rotated the device. |
screenLayout |
0x0100 |
The screen layout has changed. This might be caused by a different display being activated. |
uiMode |
0x0200 |
The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc. |
fontScale |
0x40000000 |
The font scaling factor has changed, that is the user has selected a new global font size. |
5>android:screenOrientation的可选属性值
Constant Value Description unspecified -1 No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device landscape 0 Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data. portrait 1 Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data. user 2 Use the user's current preferred orientation of the handset. behind 3 Keep the screen in the same orientation as whatever is behind this activity. sensor 4 Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. nosensor 5 Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device. sensorLandscape 6 Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing. sensorPortait 7 Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing. reverseLandscape 8 Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape. reversePortait 9 Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait. fullSensor 10 Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation).
发表评论
-
Android获取sdcard信息
2011-04-04 22:11 39771>实例代码: if (Environ ... -
Android获取通话状态
2011-04-04 21:08 72831>添加读取手机状态的权限 <uses-pe ... -
Android 系统服务getSystemService
2011-04-02 21:04 3781public Object getSystemService ... -
Android获取SIM卡信息--TelephonyManager
2011-04-02 19:37 82551>获得TelephonyManager Telepho ... -
Android获取、设置Wifi状态
2011-04-02 19:14 54001>获得WifiManager WifiManager ... -
Android获取、设置桌面
2011-04-02 18:57 14921>添加权限 <uses-permission ... -
Android SQLite学习工具
2011-03-27 19:01 1204Android自带数据库SQLite,是一个超级迷你,但是五脏 ... -
自定义程序实现Android EditText只允许输入指定字符
2011-03-24 19:18 7261程序设计的很多地方都要用到一个小技术:指定文本框的输入类型。即 ... -
Android EditText禁止输入中文字符
2011-03-24 16:51 27355只允许EditText输入数字或者email格式字符是非常简 ... -
Android 自定义拨打电话程序段
2011-03-17 14:09 1947strInput = "13912345 ... -
java合并两个XML文件
2010-09-13 15:59 9915package cs.edu.hust.cs.j2 ... -
sss
2010-09-09 12:09 0getWindow().setFlags(WindowMana ... -
android 邮件地址正则表达式
2010-09-07 20:24 1832public static boolean isEma ... -
正则表达式需要转义的字符
2010-09-07 20:08 8244$ ---> \$ ( ---> \ ... -
Android手机号码判断正则表达式
2010-09-07 19:26 9947public static boolean isPho ... -
Android发送短信与邮件
2010-09-07 19:14 3119发送短信: 注意引入包的时候,应该是: import andr ... -
Android TabHost动态加载内容总结
2010-09-02 16:16 5761前面用继承TabActivity的方法很好的完成了,TabHo ... -
Android TabHost使用、动态加载内容
2010-09-02 16:03 7595使用TabHost有两种办法 1.在layout的xml文件里 ... -
Android Activity间传递对象startActivityForResult、onActivityResult、setResult总结
2010-09-01 13:12 4832前面做了在Activity之间传递参数的测试。 setResu ... -
Android Activity间传递自定义类的对象
2010-09-01 12:53 13619有很多时候都需要在不同的Activity之间传递数据。 实现方 ...
相关推荐
Android 屏幕旋转实例,改变屏幕方向,这个和平时的锁定屏幕方向有关联,其基本的实现思路如下: public void onConfigurationChanged(Configuration newConfig) { Toast.makeText(this, "系统的屏幕方向发生...
android改变变屏幕方向.docx
android Activity始终横屏、全屏、屏幕方向改变等屏幕相关
Android强制横竖屏切换的一个工具代码,灰常的简单实用~
主要介绍了Android改变手机屏幕朝向的方法,涉及Android动态更改手机屏幕朝向的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
客户需要根据自己的产品设计及应用环境,切换屏幕的显示方向,或者固定好一个显示方向,例如产品中使用不同分辨率的显示屏,或者显示屏在产品中的固定方向发生改变等等,都需要进行屏幕旋转功能。 那么如何设置屏幕的...
在这里我们就可以为大家详细介绍一下有关Android屏幕大小的自适应方式,帮助大家理解。不同的Androidtarget会有不同的大小,应用程序的界面需要针对不同的大小调整界面元素的尺寸。而且Android屏幕大小也可以在横屏...
这个app展示了在屏幕方向改变的时候app经历的步骤。
“屏幕方向”(orientation)是一个Configuration,通过查看Configuration类的javadoc可以看到其他Configuration还有哪些:如fontScale、keyboardHidden和locale等等。 当屏幕旋转时,这个Configuration就发生了...
� A ndroid 在设计初期就考虑了与现其有业务的融合,改变以往从计算机为主改成从手机使用为导向。新 生应用如:G oogle 地图及其衍生应用、 GMail 、 GTalk 等。 � GPS 卫星导航功能,手机照相, MP3 ,蓝芽等均...
当屏幕转动切换的时候 Android 机制是:销毁当前屏幕的 Activity ,然后重新开启一个新的适应屏幕改变的 Activity 。那么,我们该如何在屏幕切换的...表示在改变屏幕方向、弹出软件盘和隐藏软键盘时,不再去执行 onCrea
在屏幕上滑动以改变方向并引导点穿过迷宫。 MAZE 400:你在这个流行的游戏的任务很简单:找到出口,逃离迷宫!在屏幕上滑动以改变方向并引导点穿过迷宫。从三种模式中选择一种适合您的偏好: * 经典模式与越来越困难...
(4)在用户改变手机方向导致屏幕在横屏和竖屏之间切换时,能够根据实际情况对界面布局进行调整。 (5)如果不能查询到相应城市的天气信息,则需要进行异常判断和处理 (6)提供更换皮肤的功能(让用户在内置皮肤或...
2.2.3 秘诀4:强制屏幕方向 26 2.2.4 秘诀5:保存和恢复activity信息 27 2.3 多个activity 28 2.3.1 秘诀6:使用按钮和文本框 28 2.3.2 秘诀7:通过事件启动另外一个activity 29 2.3.3 秘诀8:将语音转换成文本并...
5.18 变换屏幕的方向 5.19 获取设备信息 5.20 小结 第6章 手机自动服务 6.1 实现短信提醒 6.2 剩余电量提醒 6.3 短信群发 6.4 短信e-mail通知 6.5 来电提醒 6.5.1 实现原理 6.5.2 telephonymanager和...
5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...
5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...