在 Android 中以编程方式启用/禁用屏幕旋转
我有一个应用程序,它显示大量文本供用户阅读。
我发现,当躺着阅读时,即使我的头和屏幕对齐,屏幕也会旋转,这让我很恼火。
我不想将其设置为永久处于纵向模式,因此我认为这将排除
android:screenOrientation="portrait"
在清单中设置的方法。
理想情况下,我想通过首选项页面启用/禁用自动方向更改。
我最好的方法是什么?
I have an app which displays a large amount of text for the user to read.
I've found that when reading while lying down, I get annoyed that the screen rotates even though my head and the screen are aligned.
I do not want to set this to be permanently in portrait mode, so I think this would preclude an approach of setting the
android:screenOrientation="portrait"
in the manifest.
Ideally, I would like to enable/disable automatic orientation changes via a preference page.
What would be my best approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过将我的首选项页面写入我的 SharedPreferences 来做到这一点应用程序,然后读取此值并调用 Activity.setRequestedOrientation()当加载或显示相关文本视图时,从代码中获取 。
例如:
这可以在
onCreate()
或onResume()
方法中。不在onCreate()
方法中使用它的唯一原因是,如果设置更改发生在此活动中,或者发生在完成后将返回到此活动的子活动上。编辑:
好的,如果这不起作用(我不太确定它会起作用),也许您可以尝试使用两个不同的布局 xml 文件 - 一个设置了 screenOrientation,另一个没有设置。当您加载活动/视图时,您可以根据保存的首选项动态加载其中之一。它
令人讨厌不干净,但它应该可以工作。I would do this by having my preference page write to the SharedPreferences for my application, then reading this value and calling Activity.setRequestedOrientation() from the code when the text view in question is loaded or displayed.
For example:
This can be in either the
onCreate()
oronResume()
method. The only reason not to have it in theonCreate()
method is if the changing of the setting happens in this activity, or on a child activity that will come back to this one when it is done.Edit:
OK, if that doesn't work (and I am not really sure it will), maybe you could try having two different layout xml files - one with the screenOrientation set, and the other without. When you load your Activity/View, you can dynamically load one or the other based on your saved preferences. It's
nastynot clean, but it should work.在 onCreate() 函数中添加此行。它正在工作...
Add This line inside onCreate() Function. It is working ...