How do I disable orientation change on Android?

后端 未结 12 1158
一向
一向 2020-11-22 02:45

I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation=\"portrait\" in the manifest XML. This works OK for the HTC

12条回答
  •  一整个雨季
    2020-11-22 02:56

    As said, set android:configChanges of your Activity (in manifest file) to keyboardHidden|orientation and then:

    1) Override onConfigurationChanged()

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //here you can handle orientation change
    }
    

    2) Add this line to your activity's onCreate()

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    It's better than add same line to onConfigurationChanged, because your app will turn to portrait mode and then back to landscape (it will happen only one time, but it's annoying).

    Also you can set android:screenOrientation="nosensor" for your activity (in manifest). But using this way you're a not able to handle orientation changes at all.

提交回复
热议问题