Android 如何让应用程序忽略屏幕方向变化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1410504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make an application ignore screen orientation change?
提问by Lyubomyr Dutko
Is there a way to make an application completely ignore a screen orientation change?
有没有办法让应用程序完全忽略屏幕方向的变化?
采纳答案by Marcin Gil
It is possible, quite easily, to override the default behavior and forbid a screen orientation change when the keyboard is open/closed.
当键盘打开/关闭时,可以很容易地覆盖默认行为并禁止屏幕方向更改。
Modifying the manifest
修改清单
Open the manifest, switch to the Application tab and select the desired Activity you wish to override for the orientation change behavior.
打开清单,切换到应用程序选项卡,然后选择您希望为方向更改行为覆盖的所需活动。
Within Attributes you need to change two fields: Screen orientation: select either portrait or landscape - whichever is desired. This will be the default layout.
Select events for Config changes you wish to override: In this case these are keyboardHidden and orientation.
在 Attributes 中,您需要更改两个字段: 屏幕方向:选择纵向或横向 - 任意选择。这将是默认布局。
为您希望覆盖的配置更改选择事件:在这种情况下,这些事件是 keyboardHidden 和方向。
Modifying the Activity implementation
修改活动实现
Now you need to override a single function within desired Activity.
现在您需要覆盖所需活动中的单个函数。
Just add the function below to your Activity's class.
只需将下面的函数添加到您的 Activity 类中即可。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
This is the default implementation if using the Source->Override/Implement Methods
menu option.
如果使用Source->Override/Implement Methods
菜单选项,这是默认实现。
That's it! Now your orientation will always be kept.
就是这样!现在您的方向将始终保持不变。
Remember that this setting is per Activity - so you need to repeat this step for each Activity you wish to forbid the orientation change!
请记住,此设置是针对每个 Activity 的 - 因此您需要为您希望禁止方向更改的每个 Activity 重复此步骤!
(Based on SDK 1.1)
(基于 SDK 1.1)
回答by haseman
You can make the same change in code with the following line (called in an activity):
您可以使用以下行(在活动中调用)对代码进行相同的更改:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Once you make this call, your application will stay in landscape (or portrait) mode. You can use the same call (with a different ActivityInfo enum) to make it sensitive to the orientation shifting again.
进行此调用后,您的应用程序将保持横向(或纵向)模式。您可以使用相同的调用(使用不同的 ActivityInfo 枚举)使其再次对方向转换敏感。
There's a full DevX article on the topic in Developing Orientation-Aware Android Applications.
开发定向感知 Android 应用程序中有一篇关于该主题的完整 DevX 文章。
(WARNING: since I've posted this link DevX has put up a registration wall.)
(警告:自从我发布了这个链接后,DevX 已经设置了一个注册墙。)
回答by bytebender
If you are setting either by AndroidManifest.xml
or with the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
you are going to run into issues with tablets. Their natural/default orientation is landscape.
如果您通过AndroidManifest.xml
或进行设置,setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
您将遇到平板电脑的问题。它们的自然/默认方向是横向。
If you truly want to completely ignore screen orientation changesI would use this setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
value. I talk more about it in Stack Overflow question Android natural sensor orientation help.
如果你真的想完全忽略屏幕方向的变化,我会使用这个setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
值。我在 Stack Overflow 问题Android 自然传感器方向帮助中详细讨论了它。
Here is the xml:
这是xml:
<activity
android:name=".MyActivity"
android:screenOrientation="nosensor"
android:configChanges="orientation|keyboardHidden|keyboard"/>
回答by FeelGood
You can define your activity in the AndroidManifest.xml
file like this:
您可以在AndroidManifest.xml
文件中定义您的活动,如下所示:
<activity
android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard"/>`
In this case you should set the property for each activity. I didn't find an inline solution for all applications.
在这种情况下,您应该为每个活动设置属性。我没有找到适用于所有应用程序的内联解决方案。
回答by FeelGood
<activity android:screenOrientation="portrait"></activity>
回答by Don Larynx
Add this to the activity:
将此添加到活动中:
android:configChanges="orientation|screenSize"
android:configChanges="orientation|screenSize"
回答by Mic
You want to read the current orientation and keep it this way throughout all the activity's lifetime, so what I did is the following, at the end of onCreate:
您想读取当前方向并在整个 Activity 的整个生命周期中保持这种方式,所以我在 onCreate 结束时做了以下操作:
// choose an orientation and stay in it
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);