问题描述
我有一个应用程序,它显示大量的文本供用户阅读。
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.
我不希望将它设置为永久在纵向模式下,所以我认为这将preclude设置的办法
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"
在清单中。
在理想情况下,我想启用/禁用通过preference页自动方向改变。
Ideally, I would like to enable/disable automatic orientation changes via a preference page.
什么是我的最好的方法?
What would be my best approach?
推荐答案
我会通过让我的preference页写的<一个做到这一点href="http://developer.android.com/reference/android/content/Shared$p$pferences.html">Shared$p$pferences我的应用程序,然后读取这个值并调用Activity.setRequestedOrientation()从什么时候有问题的文本视图加载或显示在code。
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.
例如:
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()
的方法是,如果设置的变化发生在这个活动中,或在孩子的活动,将回到这一个当它完成
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.
编辑:
OK,如果不工作,(我真的不知道它会),也许你可以试试有两个不同的布局XML文件 - 一个与screenOrientation集,没有其他。当你加载你的Activity / View,您可以动态加载一个或其他根据您节省preferences。这是讨厌击>不干净,但它应该工作。
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 not clean, but it should work.
这篇关于通过编程方式启用/禁用屏幕旋转Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!