android:screenOrientation =“portrait”在Jellybean中被忽略

时间:2012-12-12 16:48:39

标签: android screen-orientation android-4.2-jelly-bean

android:screenOrientation =“portrait”在Jellybean中被忽略。如果我将手机变成横向,它将改变方向,无论如何。我试过几款手机。我必须使用新的旗帜吗?

1 个答案:

答案 0 :(得分:2)

您可以尝试以编程方式执行此操作,

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    switch (newConfig.orientation)
    {
    case Configuration.ORIENTATION_PORTRAIT:
        // taking action on event
        lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
    break;
    case Configuration.ORIENTATION_LANDSCAPE:
        // taking action on event
        lockScreenRotation(Configuration.ORIENTATION_LANDSCAPE);
    break;
    case Configuration.ORIENTATION_SQUARE:
        // taking action on event
        lockScreenRotation(Configuration.ORIENTATION_SQUARE);
    break;
    default:
        throw new Exception("Unexpected orientation!!!");
    break;
 }




private void lockScreenRotation(int orientation)
    {
      // Stop the screen orientation changing during an event
        switch (orientation)
        {
        ...
      case Configuration.ORIENTATION_PORTRAIT:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
      case Configuration.ORIENTATION_LANDSCAPE:
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        break;
        ...
      case default:
       this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
       break;
        }
    }