Android:如何保持Activity开始时的方向不变



我知道如何锁定一个特定方向的活动(在AndroidManifest.xml):

android:screenOrientation="landscape|portrait"

我知道如何通过编程锁定到特定的方向:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

但是,我如何将活动锁定到开始时的方向?例如,如果它开始纵向,它应该坚持。

谢谢!

在应用程序启动时使用getResources().getConfiguration().orientation,然后像上面那样编程地设置方向。该方法将返回ORIENTATION_LANDSCAPEORIENTATION_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"

最新更新