如何在运行时locking方向

有没有办法在运行时locking方向? 例如,如果用户当前处于横向并切换菜单选项,则希望允许用户将屏幕locking为横向。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

调用一个活动,将其locking到风景。 查找ActivityInfo类中的其他标志。 您可以将其locking到纵向或使其传感器/滑块驱动。

更多信息在这里: http : //www.devx.com/wireless/Article/40792

注意getConfiguration返回和setRequestedOrientation需要什么之间的区别 – 它们都是int,但它们来自不同的常量定义。

以下是如何locking当前的方向,同时允许180度的翻转

 int currentOrientation = getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } 

这适用于具有反向纵向和反向横向的设备。

locking方向:

  int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation); 

解锁方向:

  getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

我似乎也有类似的情况。 我想支持任何方向,但是我需要在工作stream程中的某个点之后保持当前的方向。 我的解决scheme是:

进入受保护的工作stream程时:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

在受保护工作stream程的退出时:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

替代@pstoppani答案支持平板电脑(与@pstoppani答案,这将只适用于设备> 2.2)
– 在Samsung Galaxy SIIISamsung Galaxy Tab 10.1

 public static void lockOrientation(Activity activity) { Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation(); int tempOrientation = activity.getResources().getConfiguration().orientation; int orientation = 0; switch(tempOrientation) { case Configuration.ORIENTATION_LANDSCAPE: if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; else orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; case Configuration.ORIENTATION_PORTRAIT: if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; else orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } activity.setRequestedOrientation(orientation); } 

这里是我的代码,你可以用这些方法之一locking你的屏幕,一旦完成任务解锁它与unlockOrientation:

 /** Static methods related to device orientation. */ public class OrientationUtils { private OrientationUtils() {} /** Locks the device window in landscape mode. */ public static void lockOrientationLandscape(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } /** Locks the device window in portrait mode. */ public static void lockOrientationPortrait(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } /** Locks the device window in actual screen mode. */ public static void lockOrientation(Activity activity) { final int orientation = activity.getResources().getConfiguration().orientation; final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); // Copied from Android docs, since we don't have these values in Froyo 2.2 int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8; int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO if (!BuildVersionUtils.hasGingerbread()) { SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){ if (orientation == Configuration.ORIENTATION_PORTRAIT){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) { if (orientation == Configuration.ORIENTATION_PORTRAIT){ activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } } /** Unlocks the device window in user defined screen mode. */ public static void unlockOrientation(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); } } 

以上是@pstoppani上面答案的Xamarin转换。

注:这是一个片段,取代活动。此。 如果在一个活动中使用。

 public void LockRotation() { ScreenOrientation orientation; var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation; switch (surfaceOrientation) { case SurfaceOrientation.Rotation0: orientation = ScreenOrientation.Portrait; break; case SurfaceOrientation.Rotation90: orientation = ScreenOrientation.Landscape; break; case SurfaceOrientation.Rotation180: orientation = ScreenOrientation.ReversePortrait; break; default: orientation = ScreenOrientation.ReverseLandscape; break; } Activity.RequestedOrientation = orientation; } public void UnlockRotation() { Activity.RequestedOrientation = ScreenOrientation.Unspecified; } 

这是未经testing的,在使用之前采用了不同的方法,但可能是有用的。