如何禁用方向变化的Andr​​oid?方向、Andr、oid

2023-09-12 21:29:02 作者:小猪丁丁

我有,我只是想在纵向模式下使用的应用程序,所以我已经定义 机器人:在清单XML screenOrientation =肖像。该方法适用于HTC Magic的手机确定(和prevents取向对其他手机的变化以及)。

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation="portrait" in the manifest XML. This works OK for the HTC magic phone (and prevents orientation changes on other phones as well).

不过,我与HTC G1手机的问题,因为我打开硬件QWERTY键盘(而不是虚拟键盘)。我的活动停留在纵向模式,但似乎得到重启,失去了所有的状态。这不会发生与主人公的版本。

But I have a problem with the HTC G1 phone as i open the hardware qwerty keyboard (not the virtual keyboard). My activity stays in portrait mode, but seems to get restarted and loses all its states. This does not happen with the hero version.

我的应用程序是相当大的,所以我不希望它重新启动并失去所有国家键盘打开时。我如何能prevent任何想法?

My application is quite big so I don't want it to restart and lose all its states when the keyboard is opened. Any idea on how I can prevent that?

推荐答案

更新2013年4月:不要这样做。这不是一个好主意,在2009年,当我第一次回答了这个问题,它真的不是一个好主意了。看到这个答案由hackbod的原因:http://stackoverflow.com/a/5336057/84021

添加安卓configChanges =keyboardHidden |定位你的Andr​​oidManifest.xml中。这告诉系统哪些配置更改你要自己处理 - 无为而在这种情况下

Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

<activity android:name="MainActivity"
     android:screenOrientation="portrait"
     android:configChanges="keyboardHidden|orientation">

请参阅http://developer.android.com/reference/android/R.attr.html#configChanges了解更多详情。

不过,您的应用程序可以在任何时间,如中断一个电话,让您真正应该增加code保存您的应用程序的状态时,它已暂停。

However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.

更新:作为的Andr​​oid 3.2,你还需要添加屏幕尺寸:

Update: As of Android 3.2, you also need to add "screenSize":

<activity
    android:name="MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">

从http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange:

注意:与Android 3.2(API级别13),在屏幕尺寸开始   也改变时,该设备纵向和横向之间切换   方向。因此,如果你想prevent运行时重新启动因   方向变化开发API级别13或更高(如当   由的minSdkVersion声明,targetSdkVersion属性),你   必须包括另外的屏幕尺寸价值的方向   值。也就是说,你必须声明   安卓configChanges =方向|屏幕尺寸。但是,如果你的   应用目标API级别12或更低,那么你总是活动   处理这种结构变化本身(此配置更改   在Android 3.2或运行时,不会重新启动您的活动,甚至   更高的设备)。

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must declare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).