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

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

问题描述

我有一个应用程序,我只想在纵向模式下使用,所以我定义了android:screenOrientation="portrait" 在清单 XML 中.这适用于 HTC Magic 手机(也可以防止其他手机的方向变化).

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

我的应用程序很大,所以我不希望它在键盘打开时重新启动并丢失所有状态.我怎样才能防止这种情况发生?

解决方案

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

避免使用 asynctask 重新加载 Activity关于android中的方向变化

android:configChanges="keyboardHidden|orientation" 添加到您的 AndroidManifest.xml.这会告诉系统您将自己处理哪些配置更改 - 在本例中什么都不做.

有关详细信息,请参阅开发者参考 configChanges.>

但是,您的应用程序可以随时中断,例如通过电话,所以你真的应该添加代码来保存你的应用程序暂停时的状态.

更新:从 Android 3.2 开始,您还需要添加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天全站免登陆