锁屏方向(Android)

发布于 2024-10-11 10:16:43 字数 384 浏览 2 评论 0原文

我正在编写一个 Android 应用程序,它使用具有不同内容(活动)的选项卡。 在其中一项活动中,我想将屏幕方向锁定为“横向”模式, 但在其他活动中,我想要正常方向(根据传感器)。

我现在正在做的是,

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

当我切换到横向模式活动以及

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

切换回其他活动时,我会进行调用。然而,这似乎不起作用, 整个应用程序被锁定。解决这个问题的正常方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雾里花 2024-10-18 10:16:44

在清单中,您可以将 screenOrientation 设置为横向。它在 XML 中看起来像这样:

<activity android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
...
</activity>

其中 MyActivity 是您想要保持在横向状态的活动。

android:configChanges=... 行可防止在屏幕旋转时调用 onResume()onPause()。如果没有这条线,轮换将按照您的要求进行,但仍会进行呼叫。

注意: keyboardHiddenorientationkeyboardHiddenorientation 所必需的。 Android 3.2(API 级别 13),并且所有三个选项都需要 3.2 或更高版本,而不仅仅是 orientation

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>

Where MyActivity is the one you want to stay in landscape.

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.

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.

强者自强 2024-10-18 10:16:44

我有类似的问题。

当我

<activity android:name="MyActivity" android:screenOrientation="landscape"></activity>

在清单文件中输入时,这导致该活动以横向方式显示。然而,当我返回之前的活动时,即使它们被设置为纵向,它们也会以横向显示。但是,通过在目标活动的 OnCreate 部分之后立即添加

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

可以解决问题。所以我现在两种方法都用。

I had a similar problem.

When I entered

<activity android:name="MyActivity" android:screenOrientation="landscape"></activity>

In the manifest file this caused that activity to display in landscape. However when I returned to previous activities they displayed in lanscape even though they were set to portrait. However by adding

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

immediately after the OnCreate section of the target activity resolved the problem. So I now use both methods.

无法回应 2024-10-18 10:16:44

在项目的 Android 清单文件中,找到要修复方向的活动声明并添加以下代码,

android:screenOrientation="landscape"

对于横向方向和纵向添加以下代码,

android:screenOrientation="portrait"

inside the Android manifest file of your project, find the activity declaration of whose you want to fix the orientation and add the following piece of code ,

android:screenOrientation="landscape"

for landscape orientation and for portrait add the following code,

android:screenOrientation="portrait"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文