锁定屏幕方向 (Android) [英] Lock screen orientation (Android)
问题描述
我正在编写一个 android 应用程序,该应用程序使用具有不同内容(活动)的选项卡.在其中一项活动中,我想将屏幕方向锁定为横向"模式,但在其他活动中,我想要正常方向(根据传感器).
I'm writing an android application that uses tabs with different contents (activities). In one of these activities, I would like to lock the screen orientation to "Landscape"-mode, but in the other activities, I want the normal orientation (according to sensor).
我现在正在做的是打电话
What I'm doing now is that I'm calling
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
当我切换到横向模式活动时,
when I switch to the landscape mode activity, and
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
当我切换回其他活动时.然而,这似乎不起作用,整个应用程序锁定.解决此问题的常规方法是什么?
when I switch back to the other activities. However, this doesn't seem to work, the whole application locks up. What is the normal approach to this problem?
推荐答案
在 Manifest 中,您可以将 screenOrientation 设置为横向.它在 XML 中看起来像这样:
In the Manifest, you can set the screenOrientation to landscape. It would look something like this in the XML:
<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>
其中 MyActivity
是您希望保持横向的活动.
Where MyActivity
is the one you want to stay in landscape.
android:configChanges=...
行防止 onResume()
、onPause()
在屏幕旋转时被调用.如果没有这条线,轮换将按照您的要求保持,但仍会进行调用.
The android:configChanges=...
line prevents onResume()
, onPause()
from being called when the screen is rotated. Without this line, the rotation will stay as you requested but the calls will still be made.
注意:keyboardHidden
和 orientation
是 < 所必需的.Android 3.2(API 级别 13),并且三个选项都需要 3.2 或更高版本,而不仅仅是orientation
.
Note: keyboardHidden
and orientation
are required for < Android 3.2 (API level 13), and all three options are required 3.2 or above, not just orientation
.
这篇关于锁定屏幕方向 (Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!