如何在Android上禁用方向更改? [英] How do I disable orientation change on Android?

查看:81
本文介绍了如何在Android上禁用方向更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只想在人像模式下使用的应用程序,所以我已经定义了 清单XML中的android:screenOrientation ="portrait".对于 HTC Magic 电话,它可以正常运行(并且还可以防止其他电话的方向更改)./p>

但是我打开硬件 HTC G1 手机有问题="https://en.wikipedia.org/wiki/QWERTY" rel ="noreferrer"> QWERTY键盘(不是虚拟键盘).我的活动保持纵向模式,但似乎重新启动并失去了所有状态. HTC Hero 版本不会发生这种情况.

我的应用程序很大,所以我不希望它重新启动并在打开键盘时丢失所有状态.我该如何预防?

解决方案

2013年4月更新:请勿执行此操作.在2009年我第一次回答这个问题时,这不是一个好主意,现在确实不是一个好主意.出于以下原因,请参阅hackbod的答案:

避免通过asynctask重新加载活动android中的方向更改

android:configChanges="keyboardHidden|orientation"添加到您的AndroidManifest.xml中.这告诉系统您将要处理的配置更改-在这种情况下,什么也不做.

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

有关更多详细信息,请参见开发人员参考 configChanges .

>

但是,您的应用程序可以随时中断,例如通过电话,因此您确实应该添加代码以在应用程序暂停时保存其状态.

更新:从Android 3.2开始,您还需要添加"screenSize":

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

来自开发人员指南自行处理配置更改

警告:从Android 3.2(API级别13)开始,屏幕尺寸" 设备在纵向和横向之间切换时也会发生变化 方向.因此,如果要防止运行时由于以下原因而重新启动, 针对API级别13或更高版本(例如, 由minSdkVersion和targetSdkVersion属性声明), 除了方向"外,还必须包括"screenSize"值 价值.也就是说,您必须声明 android:configChanges="orientation|screenSize".但是,如果您 应用定位到API级别12或更低级别,那么您的活动始终 自行处理此配置更改(此配置更改 即使在Android 3.2或更高版本上运行,也不会重新启动您的活动 更高的设备).

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).

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 it seems to get restarted and loses all its states. This does not happen with the HTC Hero version.

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

解决方案

Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:

Avoid reloading activity with asynctask on orientation change in android

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">

See Developer reference configChanges for more details.

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.

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

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

From Developer guide Handling the Configuration Change Yourself

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).

这篇关于如何在Android上禁用方向更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆