锁屏方向(Android)
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在清单中,您可以将 screenOrientation 设置为横向。它在 XML 中看起来像这样:
其中
MyActivity
是您想要保持在横向状态的活动。android:configChanges=...
行可防止在屏幕旋转时调用onResume()
、onPause()
。如果没有这条线,轮换将按照您的要求进行,但仍会进行呼叫。注意:
keyboardHidden
和orientation
是keyboardHidden
和orientation
所必需的。 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:
Where
MyActivity
is the one you want to stay in landscape.The
android:configChanges=...
line preventsonResume()
,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
andorientation
are required for < Android 3.2 (API level 13), and all three options are required 3.2 or above, not justorientation
.我有类似的问题。
当我
在清单文件中输入时,这导致该活动以横向方式显示。然而,当我返回之前的活动时,即使它们被设置为纵向,它们也会以横向显示。但是,通过在目标活动的 OnCreate 部分之后立即添加
可以解决问题。所以我现在两种方法都用。
I had a similar problem.
When I entered
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
immediately after the OnCreate section of the target activity resolved the problem. So I now use both methods.
在项目的 Android 清单文件中,找到要修复方向的活动声明并添加以下代码,
对于横向方向和纵向添加以下代码,
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 ,
for landscape orientation and for portrait add the following code,