有条件地处理方向变化
我添加了 android:configChanges="orientation"
并重写了 onConfigurationChanged()
。我想做的是仅当设备具有平板电脑大小的显示分辨率设置(短边大于 480px - 欢迎提出更好的处理方法的建议!)时才允许横向方向更改。我认为这可以通过两种方式之一来实现,但在我的测试中无论是在我的手机上还是在模拟器中都不起作用。
我的第一个想法是不调用超类的 onConfigurationChanged() 实现,但我抛出了一个异常,抱怨我没有调用超类。
我的第二个想法是在将 newConfig.orientation 传递给超类之前手动将其设置为 Configuration.ORIENTATION_PORTRAIT
。在我的 Activity 和框架中的实际 Activity 类之间,我有一个 ActivityBase 类,我也使用它来覆盖它。我可以在 ActivityBase 中祝酒 newConfig.orientation 的值,以查看确实传递的配置对象确实具有新值,但它被忽略并且方向更改已经发生。
我可以在哪里放置允许我在某些情况下有条件地短路方向变化并在其他情况下允许它的代码?
I added android:configChanges="orientation"
and I override onConfigurationChanged()
. What I'd like to do is allow the landscape orientation change only if the device has a tablet-sized display resolution setting (shorter side greater than 480px - suggestions for better way of handling this are welcome!). I was thinking that this would work one of two ways, but neither is working in my tests either on my phone or in the emulator.
My first idea was to not call the super class' implementation of onConfigurationChanged()
, but I get an exception thrown that complains about me not calling the super class.
My second idea was to just manually set newConfig.orientation to Configuration.ORIENTATION_PORTRAIT
before passing it to the super class. In between my Activity and the actual Activity class in the framework I have an ActivityBase class that I use where I am overriding this as well. I can toast the value of newConfig.orientation in ActivityBase to see that the configuration object indeed being passed around does have the new value, but it is ignored and the orientation change has already occurred.
Where can I put code that allows me to conditionally short circuit the orientation change in some cases and allow it in others?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将设置请求的方向以在 Activity onCreate() 方法中修复,如下所示:
如果显示器不是 xhdmi 显示器或具有所需的分辨率。
I would set the requested orientation to fix in the Activity onCreate() method, something like this:
if the Display isn't a xhdmi Display or has the required resolution.