在 Android 中以编程方式启用/禁用屏幕旋转

发布于 2024-09-12 22:26:35 字数 247 浏览 6 评论 0原文

我有一个应用程序,它显示大量文本供用户阅读。

我发现,当躺着阅读时,即使我的头和屏幕对齐,屏幕也会旋转,这让我很恼火。

我不想将其设置为永久处于纵向模式,因此我认为这将排除

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-09-19 22:26:35

我将通过将我的首选项页面写入我的 SharedPreferences 来做到这一点应用程序,然后读取此值并调用 Activity.setRequestedOrientation()当加载或显示相关文本视图时,从代码中获取

例如:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("fix_orientation_to_portrait", false)) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

这可以在 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:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("fix_orientation_to_portrait", false)) {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    } else {
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

This can be in either the onCreate() or onResume() method. The only reason not to have it in the onCreate() 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 nasty not clean, but it should work.

九局 2024-09-19 22:26:35

在 onCreate() 函数中添加此行。它正在工作...

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Add This line inside onCreate() Function. It is working ...

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文