【问题标题】:Android - Let the activity remain in its original OrientationAndroid - 让活动保持其原始方向
【发布时间】:2012-06-11 16:21:23
【问题描述】:

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

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

int orientation = getResources().getConfiguration().orientation;
setRequestedOrientation(orientation);

如果我在 PORTRAIT 模式下打开 Activity,并尝试通过旋转手机切换到 LANDSCAPE,它不会 - 这是预期的。问题是,如果在 LANDSCAPE 中打开活动并且我尝试旋转到纵向 - 它确实如此。

我不明白为什么会发生这种情况。

代码出现在 onCreate() 的 setContentView() 上方

【问题讨论】:

  • 您是否保存了有关配置更改的方向?保存 int 然后在设置布局之前恢复它。

标签: android android-layout android-intent android-emulator android-widget


【解决方案1】:

问题在于getResources().getConfiguration().orientationsetRequestedOrientation(orientation); 使用不同的常量。您可以尝试阅读它们的文档,并在它们之间进行映射。

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多