锁定屏幕方向(安卓) [英] Lock screen orientation (Android)

查看:124
本文介绍了锁定屏幕方向(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  如何禁用Android的方向变化?

我在写一个使用标签内容不同(活动)的Andr​​oid应用程序。 在这些活动中的一个,我想锁定屏幕方向,以山水 - 模式, 但在其他的活动,我想正常的方向(根据传感器)。

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?

推荐答案

在清单中,可以设置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.

安卓configChanges = ... 行prevents 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 定位所需要的&LT;的Andr​​oid 3.2(API等级13),并且所有三个选项都需要3.2或以上,而不仅仅是定位

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.

这篇关于锁定屏幕方向(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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