【问题标题】:It is possible to known when the screen orientation is changed but with portrait mode forced on manifest?可以知道何时更改屏幕方向但在清单上强制使用纵向模式?
【发布时间】:2012-02-29 13:20:19
【问题描述】:

欢迎大家

我对我的应用有特殊需求。我必须强制应用程序仅在纵向模式下工作,但我需要知道用户何时将手机移至横向模式。为什么?因为我正在显示带有纹理图像的 opengl 视图,并且当用户将手机位置更改为横向模式时,我必须在不重置活动的情况下旋转多边形。然后我必须在清单上强制使用纵向模式,因为我不希望我的 onCreate 方法再次被调用。

拜托,谁能告诉我如何做到这一点?

我知道如何旋转图像,我只需要知道用户何时将手机移动到横向位置,但在清单上强制纵向。

我尝试在清单中添加 android:screenOrientation="portrait" 并将 android:configChanges="keyboardHidden|orientation" 添加到清单中的声明中;然后在我的活动中覆盖 onConfigurationChanged() 。但这不起作用,因为竖屏模式是forzed,所以永远不会调用onConfigurationChanged方法......

谢谢

【问题讨论】:

  • 试试看:从 Manifest 中删除你的 android:screenOrientation="portrait"。添加 android:configChanges="keyboardHidden|orientation",并覆盖你的 onConfigurationChanged()。这样你的 onCreate 就不会被调用两次。
  • Derzu,你能帮我解释一下ashok的答案吗?谢谢

标签: android screen-orientation


【解决方案1】:

为什么不使用SensorManager 来监控手机何时旋转了 90 度。其实this 也可能有帮助。

【讨论】:

    【解决方案2】:

    在清单 android:configChanges="orientation" 中使用此属性会停止重新创建活动,然后覆盖 onConfigurationChange() 方法。在纵向和横向模式下提供相同的布局。当方向改变发生时,该方法将被调用然后改变你喜欢的图像

    【讨论】:

    • 拜托,谁能解释一下如何做到这一点:“在纵向和横向模式下都提供相同的布局。”
    【解决方案3】:

    不要在清单中设置屏幕方向 (android:screenOrientation="portrait")。添加 android:configChanges="keyboardHidden|orientation",并覆盖你的 onConfigurationChanged()。这样你的 onCreate 就不会被调用两次。

    这是您的 onConfigurationChanged 示例:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig); // tem que ter
        if (getResources().getConfiguration().orientation ==  Configuration.ORIENTATION_LANDSCAPE) {
    
        } else 
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多