我知道如何锁定一个活动在特定的方向(在 AndroidManifest.xml 中):
android:screenOrientation="landscape|portrait"
我知道如何通过程序锁定特定方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
但是,我该如何将活动锁定在启动时的方向上?例如,如果它以纵向开始,则应该保持这种方式。
谢谢!
我知道如何锁定一个活动在特定的方向(在 AndroidManifest.xml 中):
android:screenOrientation="landscape|portrait"
我知道如何通过程序锁定特定方向:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
但是,我该如何将活动锁定在启动时的方向上?例如,如果它以纵向开始,则应该保持这种方式。
谢谢!
在应用程序启动时使用getResources().getConfiguration().orientation
,然后像上面一样以编程方式设置方向。该方法将返回ORIENTATION_LANDSCAPE
或ORIENTATION_PORTRAIT
。
switch (((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation()) {
case Surface.ROTATION_90:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case Surface.ROTATION_180:
setRequestedOrientation(9/* reversePortait */);
break;
case Surface.ROTATION_270:
setRequestedOrientation(8/* reverseLandscape */);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
这是我的做法:
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation"
在清单文件中的活动