在 Android 上禁用屏幕旋转

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3329135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:45:00  来源:igfitidea点击:

Disable screen rotating on Android

androidscreen-orientation

提问by davs

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.

顺便说一句,当用户单击按钮时,手机可以处于横向或纵向位置。

回答by androidworkz

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

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

回答by mdma

You can change your AndroidManifest.xml to

您可以将 AndroidManifest.xml 更改为

<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?

请参阅如何在 Android 上禁用方向更改?

回答by davs

I've found the solution:

我找到了解决方案:

in manifest.xml:

在 manifest.xml 中:

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

in MyApplication.java:

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


}