Android-让活动保持原来的方向

Android-让活动保持原来的方向,android,android-layout,android-intent,android-emulator,android-widget,Android,Android Layout,Android Intent,Android Emulator,Android Widget,当我打开一个活动时,我希望该活动保持在已打开的方向。 例如,如果我在纵向模式下打开它,用户将无法将其更改为横向,而当我在横向模式下打开它时,用户将无法更改为纵向。(我需要一个视频播放器) 为此,我获取当前屏幕方向,然后设置它,如下所示: int orientation = getResources().getConfiguration().orientation; setRequestedOrientation(orientation); 如果我在纵向模式下打开活动,并尝试通过旋转手机切换到横

当我打开一个活动时,我希望该活动保持在已打开的方向。
例如,如果我在纵向模式下打开它,用户将无法将其更改为横向,而当我在横向模式下打开它时,用户将无法更改为纵向。(我需要一个视频播放器)

为此,我获取当前屏幕方向,然后设置它,如下所示:

int orientation = getResources().getConfiguration().orientation;
setRequestedOrientation(orientation);
如果我在纵向模式下打开活动,并尝试通过旋转手机切换到横向模式,则不会-这是意料之中的。问题是,如果活动是在横向中打开的,而我尝试旋转到纵向-它确实如此

我不明白为什么会这样


代码出现在setContentView()上方的onCreate()中。问题是
getResources().getConfiguration().orientation
setRequestedOrientation(orientation)使用不同的常量。您可以尝试阅读它们的文档,并在它们之间进行映射。

尝试以下操作:

@Override
    public void onResume() {
        super.onResume();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

    }

您是否保存了有关配置更改的说明?保存int,然后在设置布局之前将其还原。