以编程方式在 Android 中的 Activity 中禁用和启用方向更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20021803/
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
Disable and enable orientation changes in an activity in Android programatically
提问by Ivan BASART
I have an App that make some background staff. When the background work is running a progress Circle is shown, if the device is rotated during this time then the Activity is "reset" and I want to avoid that.
我有一个应用程序可以让一些后台人员。当后台工作正在运行时,会显示进度圈,如果设备在此期间旋转,则 Activity 将“重置”,我想避免这种情况。
For this reason I decided disable orientation during this process. I have seen differends threads for this question but none with a valid solution, at least in my case.
出于这个原因,我决定在此过程中禁用方向。我已经看到了这个问题的不同线程,但没有一个有效的解决方案,至少在我的情况下。
The solutions posted are about fixing the activity orientation but you have to deal with the fact that the REVERSE orientations are not returned if you use:
发布的解决方案是关于修复活动方向,但您必须处理这样一个事实,即如果您使用,则不会返回 REVERSE 方向:
getResources().getConfiguration().orientation
The function above returns SCREEN_ORIENTATION_PORTRAIT both for PORTRAIT and REVERSE_PORTRAIT cases (at least in my tests).
上面的函数为 PORTRAIT 和 REVERSE_PORTRAIT 情况返回 SCREEN_ORIENTATION_PORTRAIT (至少在我的测试中)。
So at the end I used the Rotation value to deal with that, so my code "disable the rotation" is:
所以最后我使用了 Rotation 值来处理它,所以我的代码“禁用旋转”是:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
switch(rotation) {
case Surface.ROTATION_180:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case Surface.ROTATION_270:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case Surface.ROTATION_0:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
And to allow again the orientation:
并再次允许方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
That works perfectly in a device with Android 4.1.2 but in a Device with Android 4.2.1 it is not working as expected.
这在带有 Android 4.1.2 的设备中完美运行,但在带有 Android 4.2.1 的设备中它没有按预期工作。
I guess managing rotation in the activity live cycle should be a common issue but I have not been able to find a suitable solution. May be I'm looking to the wrong direction so any help is really welcomed.
我想在活动实时周期中管理轮换应该是一个常见问题,但我一直无法找到合适的解决方案。可能是我看错了方向,所以任何帮助都非常受欢迎。
Thanks in advance, Ivan.
提前致谢,伊万。
回答by ashfak
If you want to disable orientation changes of the phone then you may use this code in the manifest
如果您想禁用手机的方向更改,则可以在清单中使用此代码
<activity android:name=".class_name"
//if you want your screen in portrait
//如果你想要你的屏幕纵向
android:screenOrientation="portrait" >
//if you want you screen in landscape mode
//如果你想在横向模式下屏幕
android:screenOrientation="landscape" >
and if you want your phone to change orientation but prevent the process from restarting then you can use the onConfigurationChanged method in your class:
如果您希望手机改变方向但阻止进程重新启动,那么您可以在类中使用 onConfigurationChanged 方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation change
if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
super.onConfigurationChanged(newConfig);
}
}
回答by Hariharan
Try this..
尝试这个..
There is no wrong in your code. It's correct i checked it. Use that code in onConfigurationChanged
that may give the difference and another thing is use below code to disable the activity reset.
你的代码没有错。我查了一下是对的。使用该代码onConfigurationChanged
可能会产生差异,另一件事是使用下面的代码来禁用活动重置。
<activity
android:name="YourActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>
回答by benestar
You could override the on onConfiguratoinChangedmethod to disable orientation changes:
您可以覆盖onConfiguratoinChanged方法以禁用方向更改:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// ignore orientation change
if (newConfig.orientation != Configuration.ORIENTATION_LANDSCAPE) {
super.onConfigurationChanged(newConfig);
}
}
Do not forget to enable the orientation changed listener in your AndroidManifest.xml file:
不要忘记在您的 AndroidManifest.xml 文件中启用方向更改侦听器:
<activity android:configChanges="orientation" >
回答by Deep Dave
Simple: one line to add after Activity Name in android manifest
简单:在 android manifest 中的 Activity Name 后面添加一行
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale
This worked for me.
这对我有用。