2010-03-02 51 views
95

有沒有在運行時鎖定方向的方法?例如,如果用戶當前處於橫向並切換菜單選項,我想讓用戶鎖定屏幕以橫向顯示。如何在運行時鎖定方向

回答

119
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

調用某個活動時,將其鎖定爲橫向。在ActivityInfo類中查找其他標誌。您可以將其鎖定爲縱向或使其傳感器/滑塊驅動。

此處瞭解詳情:http://www.devx.com/wireless/Article/40792

+12

好的謝謝。這很好。 這將得到當前的方向。 getResources()。getConfiguration()。方向 – Jared 2010-03-02 21:16:53

+6

小心!您需要區分getConfiguration()返回值和setRequestedOrientation需要的值之間的差異 - 請參閱下面的答案 – 2012-05-07 19:31:54

+0

這種方法存在問題。確保你通過[這個答案](http://stackoverflow.com/a/3614089/1708390) – 2015-03-28 12:15:22

27

我似乎已經有類似的情況。我想支持任何方向,但我需要在工作流程中的某個點之後保持當前方向。我的解決辦法是:

在受保護的工作流程的條目:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

在受保護的工作流程的退出:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
+1

這不會解決OQ,至少在Android> = 16上。setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR)可能會將設備設置爲橫向,即使它是縱向的,而問題是指鎖定的方向。 – greg7gkb 2013-07-26 19:42:17

+1

給我,將它設置爲nosensor,如果我在風景中,可以將我帶回肖像模式 我使用了SCREEN_ORIENTATION_LOCKED,而且它對我很有用 – Jimmar 2014-08-31 14:48:17

+0

@JiMMaR SCREEN_ORIENTATION_LOCKED是Android> = 18的最佳方式。但是,如果您的目標位置較低,那是行不通的。我建議在下面使用jp36的答案。 – 2015-01-05 06:49:16

95

注意什麼之間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); 
} 
+11

您可能更喜歡使用SCREEN_ORIENTATION_USER_LANDSCAPE,因爲如果用戶已禁用設置中的屏幕旋轉,則不允許180度翻轉。同樣,當切換回自由旋轉時,SCREEN_ORIENTATION_USER比SCREEN_ORIENTATION_SENSOR更好,因爲後者允許自由旋轉,即使設置不符合。 – 2014-08-01 12:52:20

+0

太棒了!需要補充的是,當您切換到反向時,不會發生重新配置。至少在我測試過的設備上。確定是否要在對話框顯示等過程中停止重新配置非常重要。 – sberezin 2015-07-03 10:08:52

45

這個工程上的設備能夠反向縱向和橫向反轉。

鎖方向:

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); 
+4

獲取旋轉'「返回屏幕從其」自然「方向旋轉。」[source](http://developer.android.com/參考/機器人/視圖/ Display.html#getRotation())。因此,在手機上說ROTATION_0是肖像可能是正確的,但在平板電腦上,其「自然」方向可能是橫向,ROTATION_0應該返回橫向而不是縱向。 – jp36 2013-01-28 15:11:56

+0

@ jp36,我在Nexus 7上進行了測試,其自然方向與手機相同。感謝您在更大的平板電腦上進行測試(我沒有這個功能)。 – pstoppani 2013-01-28 18:33:03

+1

正如jp36所說,它不適用於具有自然風景定位的平板電腦! – DominicM 2015-02-22 13:45:49

19

替代@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); 
} 
3

這裏是我的代碼,你可以用這些方法屏幕一把鎖,一旦完成了任務與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); 
    } 

} 
0

這裏是@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; 
} 

這是未經測試的,因爲在使用前使用了不同的方法,但可能會有用。

+0

這與pstoppani的答案相同,並且在平板電腦上會失敗。 – 2017-01-03 17:28:02

相關問題