简体   繁体   English

需要方向锁定为横向,但旋转为纵向时仍需要通知

[英]Need orientation locked to landscape, but still need notification when rotated to portrait

I am working on an Android app that requires the screen be locked to landscape orientation since the UI is designed for landscape only. 我正在使用一个Android应用程序,该应用程序要求将屏幕锁定为横向,因为UI仅针对横向而设计。 However, what's happening on the screen still needs to know when the device has been rotated to portrait mode. 但是,屏幕上发生的事情仍然需要知道何时将设备旋转到纵向模式。 If I put the following in AndroidManifest.xml: 如果我将以下内容放入AndroidManifest.xml中:

android:screenOrientation="landscape"

This prevents screen orientation change notifications (onConfigurationChanged) from being fired. 这样可以防止触发屏幕方向更改通知(onConfigurationChanged)。 But, if I set the screenOrientation to "sensor," the OS automatically rotates the UI without a way to prevent it: 但是,如果将screenOrientation设置为“ sensor”,则OS会自动旋转UI,而无法阻止UI:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    //Can't override newConfig and force landscape? wtf?
    super.onConfigurationChanged(newConfig);
}

Am I just approaching this the wrong way? 我只是以错误的方式来对待吗? How can I keep the screen landscape (preventing the OS from automatically changing the orientation) but still get notifications when the orientation is changed? 如何保持屏幕风景(防止操作系统自动更改方向),但在更改方向时仍会收到通知?

I think what you want is: 我认为您想要的是:

<activity ...
    android:configChanges="orientation"
    ...

According to documentation for Android manifest <activity> element: 根据Android清单文件的清单<activity>元素:

android:configChanges

Lists configuration changes that the activity will handle itself. 列出活动将自行处理的配置更改。 When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. 在运行时发生配置更改时,默认情况下将关闭活动并重新启动活动,但是声明具有此属性的配置将阻止活动重新启动。 Instead, the activity remains running and its onConfigurationChanged() method is called. 而是,活动保持运行状态,并调用其onConfigurationChanged()方法。

... ...

Value: "orientation" 值:“方向”

Description: The screen orientation has changed — the user has rotated the device. 说明:屏幕方向已更改-用户旋转了设备。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何锁定应用程序方向(横向或纵向)? - How is application orientation (landscape or portrait) locked? 屏幕方向被强制设置为纵向,但倾斜时活动仍将旋转为横向! 如何强制人像模式? - Screen orientation forced to portrait, but still, the activities will rotate to landscape when tilted!! How to force portrait mode? 当设备处于纵向=屏幕上的可见旋转时,平板电脑上的锁定横向方向+活动开始 - Locked landscape orientation on tablet + activity started when device in portrait = visible rotation on screen 旋转设备时,即使锁定为纵向,媒体播放器仍会在所有活动中播放 - media player still plays in all activities when device is rotated even though locked to portrait 如何检测Android设备是纵向还是横向锁定 - How to detect whether Android device is locked in portrait or landscape orientation 定向听众:人像和风景? - Listener for Orientation: Portrait and Landscape? 纵向方向的人像键盘 - Portrait Keyboard in Landscape Orientation Android-屏幕方向问题-仅需要纵向 - Android - Screen Orientation Issue - Need only portrait 即使应用程序在Landscape中被锁定,Android也会在纵向文件夹中查找资源 - Android looking for resource in portrait folder even when the app is locked in Landscape Android App方向锁定为横向,锁定设备,将其旋转为纵向并解锁,应用程序崩溃 - Android App orientation locked to landscape, locking the device, rotate it to portrait and unlock it, Application crash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM