我只是想设置一些标志,当我的方向是在景观,以便当活动在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

其他回答

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

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时

Activity.getResources().getConfiguration().orientation

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

 @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">

这就是! !