Android SetRequestRotation 在启用自动旋转时将 Activity 横向翻转为纵向

Android SetRequestRotation flips Activity landscape to portrait when auto rotate enabled

我正在尝试将 RequestOrientation 设置为纵向,但 Activity 旋转为横向并在启用自动旋转时设置为纵向。 Activity 将同时支持横向和纵向模式。 Android 7.0.

以上会出现这个问题

尝试像这样设置屏幕方向以遵循之前设置的方向

<activity
    android:name="com.my.example.MyActivity"
    android:screenOrientation="behind" >
</activity>

设置在后面.

默认 android:screenOrientationunspecified。这意味着

The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.

例如,在某些平板电脑中,系统会默认选择横向,因此系统可能会在您启动 activity 后立即自动旋转到横向,然后从横向翻转到执行 setRequestOrientation 时的肖像。

因此,为了防止系统以这种方式运行,您可以尝试为 android 清单中的每个活动选择 android:screenOrientation,例如:

<activity
    android:name="com.my.example.MyActivity"
    android:screenOrientation="locked" >
</activity>

<activity
    android:name="com.my.example.MyActivity"
    android:screenOrientation="behind" >
</activity>

取决于您的应用要求。有关这些选项和其他选项的更多信息,请参阅 Google documentation