首页 文章

如何获得当前的屏幕方向?

提问于
浏览
165

我只想在我的方向处于横向时设置一些标志,这样当在onCreate()中重新创建活动时,我可以在纵向和横向加载的内容之间切换 . 我已经有了一个处理我的布局的layout-land xml .

public void onConfigurationChanged(Configuration _newConfig) {

        if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            this.loadURLData = false;
        }

        if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            this.loadURLData = true;
        }

        super.onConfigurationChanged(_newConfig);
    }

覆盖onConfigurationChanged将阻止我的layout-land xml以横向方向加载 .

我只想在onCreate()中获取设备的当前方向 . 我怎么能得到这个?

8 回答

  • 27
    int orientation = this.getResources().getConfiguration().orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        // code for portrait mode
    } else {
        // code for landscape mode
    }
    

    this 的超类是 Context

  • 5
    Activity.getResources().getConfiguration().orientation
    
  • 411

    在某些设备中 void onConfigurationChanged() 可能会崩溃 . 用户将使用此代码获取当前屏幕方向 .

    public int getScreenOrientation()
    {
        Display getOrient = getActivity().getWindowManager().getDefaultDisplay();
        int orientation = Configuration.ORIENTATION_UNDEFINED;
        if(getOrient.getWidth()==getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_SQUARE;
        } else{ 
            if(getOrient.getWidth() < getOrient.getHeight()){
                orientation = Configuration.ORIENTATION_PORTRAIT;
            }else { 
                 orientation = Configuration.ORIENTATION_LANDSCAPE;
            }
        }
        return orientation;
    }
    

    并使用

    if (orientation==1)        // 1 for Configuration.ORIENTATION_PORTRAIT
    {                          // 2 for Configuration.ORIENTATION_LANDSCAPE
       //your code             // 0 for Configuration.ORIENTATION_SQUARE
    }
    
  • 19
    int rotation =  getWindowManager().getDefaultDisplay().getRotation();
    

    this will gives all orientation like normal and reverse

    并像处理它一样

    int angle = 0;
    switch (rotation) {
        case Surface.ROTATION_90:
            angle = -90;
            break;
        case Surface.ROTATION_180:
            angle = 180;
            break;
        case Surface.ROTATION_270:
            angle = 90;
            break;
        default:
            angle = 0;
            break;
    }
    
  • 0
    getActivity().getResources().getConfiguration().orientation
    

    此命令为Portrait返回int值1,为Landscape返回2

  • 1

    如果有人想获得meaningful方向描述(就像 reverseLandscapesensorLandscape 等传递到 onConfigurationChanged(..) 那样),只需使用getRequestedOrientation()

  • 51

    在您的活动类中使用以下方法:

    @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
    
            // Checks the orientation of the screen
            if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    
                setlogo();// Your Method
                Log.d("Daiya", "ORIENTATION_LANDSCAPE");
    
            } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
    
                setlogoForLandScape();// Your Method
                Log.d("Daiya", "ORIENTATION_PORTRAIT");
            }
        }
    

    然后,要声明您的活动处理配置更改,请编辑清单文件中的相应元素,以包含 android:configChanges 属性,该属性的值代表您要处理的配置 . android:configChanges 属性的文档中列出了可能的值(最常用的值是"orientation",以防止在屏幕方向更改时重新启动,以及"keyboardHidden"以防止在键盘可用性更改时重新启动) . 您可以通过用管道分隔属性来声明属性中的多个配置值字符 .

    <activity android:name=".MyActivity"
              android:configChanges="orientation|keyboardHidden"
              android:label="@string/app_name">
    

    就这样!!

  • 12

    如果你想覆盖onConfigurationChanged方法并仍然希望它做基本的东西,那么考虑使用super.onConfigyrationChanged()作为overriden方法中的第一个语句 .

相关问题