Android:防止以编程方式更改方向

标签 android dynamic android-studio android-orientation

我正在使用:

setRequestedOrientation(getResources().getConfiguration().orientation);

及以后:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

在任务完成之前它会阻止方向改变,但它只适用于纵向,当应用程序处于横向时它不会停止改变方向。

有什么建议吗?
哎呀~

最佳答案

我现在让它在所有情况下都能正常工作。

固定屏幕:

if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

然后再次允许旋转:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

哎呀~

关于Android:防止以编程方式更改方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26581154/

相关文章:

java - Android 进度对话框在线程中关闭时崩溃

android - File.listFiles() 因无效的 UTF-8 字符而崩溃

php - CakePHP 中的动态路由

android - 如何读取多种格式的自定义属性值?

android - Android 应用程序的安全/ protected 内容分发

android - 在 Android 应用中更改主题

dynamic - 使用 DAX 公式的 Power BI 动态列引用

c# - "Catchall"C# 中的属性?

android - 无法在 Ubuntu 中的 Android Studio 中创建 Android 虚拟设备

java - Android Studio 'Clean Project' 不工作