我只是想设置一些标志,当我的方向是在景观,以便当活动在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()中获得我的设备的当前方向。我怎么才能得到这个?


Activity.getResources().getConfiguration().orientation

在某些设备中,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
}

getActivity().getResources().getConfiguration().orientation

这个命令为纵向返回int值1,横向返回int值2


如果有人想获得有意义的方向描述(如传递给onConfigurationChanged(..)与那些reverseLandscape, sensorLandscape等),只需使用getRequestedOrientation()


int rotation =  getWindowManager().getDefaultDisplay().getRotation();

这将给出所有的方向,如正方向和反向方向

然后像这样处理

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;
}

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

当它的超类是Context时


如果你想覆盖onConfigurationChanged方法,并且仍然想让它做基本的事情,那么考虑使用super.onConfigyrationChanged()作为覆盖方法中的第一个语句。


在你的活动类中使用下面的方法:

 @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");
        }
    }

Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.

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

这就是! !


我只是想提出另一种可能会让你们有些人担心的选择:

android:configChanges="orientation|keyboardHidden" 

暗示显式声明要注入的布局。

如果我们想要保留自动注入多亏了layout-land和布局文件夹。你所要做的就是将它添加到onCreate:

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getSupportActionBar().hide();

    } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        getSupportActionBar().show();
    }

在这里,我们根据手机的方向显示或不显示动作栏