从应用程序平滑更改 SDK 的方向
我知道与方向相关的问题已被多次询问,但我面临一些奇怪的问题。 我正在开发一个 SDK,它具有一些可以从应用程序打开的活动,该 SDK 要求应用程序提供方向值,并且我正在通过设置更新活动的 onCreate() 中的方向:
requestedOrientation = orientation
当我将设备保持在与应用程序设置的方向相同的方向时,效果非常好。但是,如果我以纵向模式握住设备并将 requestedOrientation
设置为 LANDSCAPE,那么我的屏幕会闪烁,首先显示纵向屏幕,然后转到风景。 在清单中,我已将 configChanges 设置为 android:configChanges="keyboard|keyboardHidden|screenSize"
我对两个方向都有单独的布局。如果还需要其他任何信息,请告诉我。
I know the question related to orientation has been asked multiple times, but I am facing some weird issue.
I am working on one SDK that has some Activity which can be opened from the App, The SDK asks for the orientation values from the App and I am updating the orientation in onCreate() of the Activity by setting:
requestedOrientation = orientation
It works perfectly when I am holding the device in the same orientation as the orientation is set by App. But If I am holding the device in portrait mode and setting the requestedOrientation
to LANDSCAPE then my screen flickers, 1st it shows the portrait screen and then goes to landscape.
In manifest I have set the configChanges to android:configChanges="keyboard|keyboardHidden|screenSize"
I have separate layouts for both the orientations. Let me know, if anything else is also required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
屏幕方向是一项活动功能,每个活动可能有所不同。因此,从一个活动到另一个活动,系统必须提前决定应该在哪个方向上显示该活动;这是在
requestedOrientation
发生之前进行的;因为它显示了具有默认方向设置的活动;然后,当调用 requestedOrientation 时,将使用所需的方向重新创建活动;所以onCreate()
将会以不同的方向被调用两次;这就是为什么屏幕在显示横向方向(用户所需的方向)之前会以纵向方向(系统所需的方向)闪烁的原因。在文档中,默认的
android: screenOrientation
方向为“未指定”;即,由系统决定;通常默认由设备传感器决定。
但是您可以指定为前面的第二个活动“锁定”(在清单的活动部分中),以便系统没有机会做出决定。
The screen orientation is an activity feature that can differ from activity to another. So, going from activity to another, the system has to decide ahead which orientation it should display the activity on; this is taken before
requestedOrientation
takes place; because it shows the activity with the default orientation setting; then whenrequestedOrientation
get called, the activity is recreated with the desired orientation; soonCreate()
will get called twice with different orientations; and that is why the screen flickers with the portrait orientation (the system desired one) before showing the landscape orientation (the user desired one).In documentation, the default
android:screenOrientation
orientation is "unspecified"; i.e., it's left up to the system to decide;Usually that defaults up to the device sensor.
But you can specify that to be "locked" for the second activity ahead (in the manifest's activity section) so that the system has no chance to decide that.
您应该根据配置更改设置请求的方向
如果您想同时支持纵向和反向纵向,请使用 SCREEN_ORIENTATION_SENSOR_PORTRAIT 来实现。
You should set requested orientation based on configuration changes
If you want to support portrait and reverse portrait together, do it with SCREEN_ORIENTATION_SENSOR_PORTRAIT.
我们最终实现了一个解决方案,如下所示,现在客户在他们的
manifest
文件中声明相同的 Activity,并根据他们的要求替换方向。SDK Manifest:
Client Manifest:
sdkPackage
是SDK的完整包,基本上客户端现在覆盖android:screenOrientation 并将其设置为他们想要的任何方向。
We have implemented a solution at our end as below, Now the clients are declaring the same Activity in their
manifest
file and replacing the orientation as per their requirements.SDK Manifest:
Client Manifest:
sdkPackage
is the complete package of SDK, basically the client is now overriding theandroid:screenOrientation
and setting it to whatever orientation they want.