[英]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.