如何在运行时锁定方向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
调用某个活动时,将其锁定为横向。在ActivityInfo类中查找其他标志。您可以将其锁定为纵向或使其传感器/滑块驱动。
我似乎已经有类似的情况。我想支持任何方向,但我需要在工作流程中的某个点之后保持当前方向。我的解决办法是:
在受保护的工作流程的条目:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
在受保护的工作流程的退出:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
这不会解决OQ,至少在Android> = 16上。setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)可能会将设备设置为横向,即使它是纵向的,而问题是指锁定的方向。 – greg7gkb 2013-07-26 19:42:17
给我,将它设置为nosensor,如果我在风景中,可以将我带回肖像模式 我使用了SCREEN_ORIENTATION_LOCKED,而且它对我很有用 – Jimmar 2014-08-31 14:48:17
@JiMMaR SCREEN_ORIENTATION_LOCKED是Android> = 18的最佳方式。但是,如果您的目标位置较低,那是行不通的。我建议在下面使用jp36的答案。 – 2015-01-05 06:49:16
注意什么之间getConfiguration回报率的差异,什么setRequestedOrientation希望 - 它们都是int,但它们来自不同的常量定义。
这里是如何锁定当前的定位,同时允许180度翻转
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
您可能更喜欢使用SCREEN_ORIENTATION_USER_LANDSCAPE,因为如果用户已禁用设置中的屏幕旋转,则不允许180度翻转。同样,当切换回自由旋转时,SCREEN_ORIENTATION_USER比SCREEN_ORIENTATION_SENSOR更好,因为后者允许自由旋转,即使设置不符合。 – 2014-08-01 12:52:20
太棒了!需要补充的是,当您切换到反向时,不会发生重新配置。至少在我测试过的设备上。确定是否要在对话框显示等过程中停止重新配置非常重要。 – sberezin 2015-07-03 10:08:52
这个工程上的设备能够反向纵向和横向反转。
锁方向:
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);
获取旋转'“返回屏幕从其”自然“方向旋转。”[source](http://developer.android.com/参考/机器人/视图/ Display.html#getRotation())。因此,在手机上说ROTATION_0是肖像可能是正确的,但在平板电脑上,其“自然”方向可能是横向,ROTATION_0应该返回横向而不是纵向。 – jp36 2013-01-28 15:11:56
@ jp36,我在Nexus 7上进行了测试,其自然方向与手机相同。感谢您在更大的平板电脑上进行测试(我没有这个功能)。 – pstoppani 2013-01-28 18:33:03
正如jp36所说,它不适用于具有自然风景定位的平板电脑! – DominicM 2015-02-22 13:45:49
替代@pstoppani答案与支持平板电脑(如@pstoppani答案,这将仅适用于设备> 2.2)
- 在上测试和Samsung 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);
}
这里是我的代码,你可以用这些方法屏幕一把锁,一旦完成了任务与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;
}
这是未经测试的,因为在使用前使用了不同的方法,但可能会有用。
这与pstoppani的答案相同,并且在平板电脑上会失败。 – 2017-01-03 17:28:02
好的谢谢。这很好。 这将得到当前的方向。 getResources()。getConfiguration()。方向 – Jared 2010-03-02 21:16:53
小心!您需要区分getConfiguration()返回值和setRequestedOrientation需要的值之间的差异 - 请参阅下面的答案 – 2012-05-07 19:31:54
这种方法存在问题。确保你通过[这个答案](http://stackoverflow.com/a/3614089/1708390) – 2015-03-28 12:15:22