Disable screen rotating on Android [duplicate]

2019-02-19 11:57发布

问题:

This question already has an answer here:

  • How do I disable orientation change on Android? 12 answers

When I press a button, I would like to disable screen rotation on all my activities. How can I do that?

BTW, the phone can be located in landscape or portrait position when the user click the button.

回答1:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



回答2:

You can change your AndroidManifest.xml to

<activity android:name="MainActivity" android:configChanges="orientation">

Which informs the OS that you will handle these config changes (by doing nothing.)

See How do I disable orientation change on Android?



回答3:

I've found the solution:

in manifest.xml:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:name="MyApplication">

in MyApplication.java:

public class UPackingListApplication extends Application {

    public void onConfigurationChanged(Configuration newConfig) {
        newConfig.orientation = [orientation we wanna to use (according to ActivityInfo class)];
        super.onConfigurationChanged(newConfig);
    }


}