关于android旋屏问题
在android中横屏状态为: landscape, 竖屏状态为:portait。
1. activity启动时设置横屏与竖屏:
在androidManifest.xml中设置android:screenOrientation="landscape", 如xml文件:
...
<activity android:name=".StyledText" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
...
另android:screenOrientation属性可设置为:
Constant Value Description
unspecified -1
landscape 0
portrait 1
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.
2. 读出当前activity横竖屏状态,更改当前activity横竖屏状态,如下程序所示,按下KEYCODE_DPAD_UP,更改状态:
public class xxxxxx extends Activity
{
...
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
int ro;
ro = this.getRequestedOrientation();
if( ro == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE ) {
ro = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
ro = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}
this.setRequestedOrientation(ro);
}
return true;
}
...
}
这里注意两个API函数:
getRequestedOrientation();
setRequestedOrientation();
用于检测当前屏幕状态,并设置当前屏幕状态。
调试中发现要注意一点:
通过setRequestedOrientation()方法设置屏幕方向后,方向键会跟着作相应的变化,左右键与上下键交换了。
附:
android/ophone 横竖屏切换问题的解决方案
http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=12181&highlight
在做android或者Ophone开发时,在默认情况下当屏幕从竖评变到横屏时会触发 onConfigurationChanged 事件 在默认情况下会重新加载画面并显示和横评一样的画面,这样会有2个问题,
- 布局问题,在竖屏 显示的布局 到横屏中由于宽和高发生了变化所有多少都会影响到布局(除非你定制2套画面在然后加个判断在横屏时怎么显示,在竖屏时怎么显示),当然最简单的办法就是在项目的 AndroidManifest.xml中找到你所指定的activity 中加上
- 只竖屏显示的话(android:screenOrientation="portrait")
- 只横屏显示的话(android:screenOrientation="landscape")
- 画面重新载入问题:在默认情况下横竖切换时会重新载入画面,导致一些不必要的资源浪费,更严重的是画面上保持的数据(特别是游戏方面)也都被重置了(当然你可以在重置前保存这些数据到数据库或者到文本文件中),要怎么避免在横竖切换时重新载入画面,
- 首先要在 AndroidManifest.xml中找到你所指定的activity 中加上android:configChanges="orientation|keyboardHidden"
- 然后 在activity的逻辑处理中(代码部分)去重载onConfigurationChanged事件,具体代码参考一下:
- @Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
}
- @Override
以上就可以简单的控制你的app画面的显示方式并且在显示方式不变的情况下横竖屏切换时不会重新加载画面,即优化的你的app同时用户体验也相应提高了。
以上,希望对大家有帮助。呵呵。
onConfigurationChanged:
http://blog.csdn.net/lightsoure/archive/2010/12/01/6046959.aspx
在AndroidManifest.xml里添加
<activity android:name=".YourActivityName"
android:configChanges="keyboardHidden|orientation">
在activity, override this method:-
@Override
public void onConfigurationChanged(Configuration newConfig)
{
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE)
{
//do your task
}
else if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT)
{
//do your task
}
super.onConfigurationChanged(newConfig);
}