Disable screen rotating on Android [duplicate]

2019-02-19 11:47发布

This question already has an answer here:

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.

3条回答
淡お忘
2楼-- · 2019-02-19 12:10

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?

查看更多
Lonely孤独者°
3楼-- · 2019-02-19 12:17

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

查看更多
太酷不给撩
4楼-- · 2019-02-19 12:19

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);
    }


}
查看更多
登录 后发表回答