【问题标题】:Disable the orientation change [duplicate]禁用方向更改[重复]
【发布时间】:2012-10-21 16:33:01
【问题描述】:

可能重复:
How to disable orientation change in Android?

我想在以任何方向(纵向或横向)打开画布后禁用方向更改,但是在以横向模式打开画布并更改设备方向时遇到问题,方向未禁用。

这里是代码。

public void onConfigurationChanged(Configuration newConfig) {

  super.onConfigurationChanged(newConfig);
  this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

}

【问题讨论】:

    标签: android


    【解决方案1】:

    在你的代码中试试这个...

    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        else
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    

    在 oncreate 中获取方向并为该活动设置该方向

    【讨论】:

    • 谢谢 Thirupathig,这对我很有用。
    【解决方案2】:

    使用此代码从清单中为您的活动添加固定方向:(将此代码放在活动标签中)

    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden"
    

    这样您的活动将仅以纵向模式显示

    【讨论】:

    • 感谢 Shruti,但我不想只在一个方向打开画布,它最初应该在纵向和横向模式下打开,但在打开画布后,应该禁用方向更改。如果我以纵向模式打开画布,上面的代码可以正常工作。
    • 好的,所以你可能只需要使用android:configChanges="orientation|keyboardHidden"这一行..
    【解决方案3】:

    使用:

    <activity android:name="YourActivity" android:configChanges="orientation|keyboardHidden">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多