Android的:如何坚持同一方向的活动开始方向、Android

2023-09-12 05:26:51 作者:黑色瞳孔里的一抹悲伤

我知道如何锁定特定方向的活动(在的Andr​​oidManifest.xml 的):

 安卓screenOrientation =山水|人像
 

我知道如何锁定到一个特定的方向编程方式:

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
 

不过,我怎么锁定活动中,在开始的方向?例如,如果它开始画像,应该坚持到底。

谢谢!

解决方案

找到了解决方法:

 开关(((窗口管理器)getSystemService(WINDOW_SERVICE))。getDefaultDisplay()。getRotation()){
  案例Surface.ROTATION_90:
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    打破;
  案例Surface.ROTATION_180:
    setRequestedOrientation(9 / * reversePortait * /);
    打破;
  案例Surface.ROTATION_270:
    setRequestedOrientation(8 / * reverseLandscape * /);
    打破;
  默认 :
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
 
Android One手机试玩 一个前景光明的开端

发现这里。

I know how to lock an activity in a specific orientation (in AndroidManifest.xml):

android:screenOrientation="landscape|portrait"

I know how to lock to a specific orientation programmatically:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

But, how do I lock the activity to the orientation in which started at? e.g., if it started portrait, it should stick to that.

Thanks!

解决方案

Found a solution:

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);
}

Found it here.