我如何禁用景观模式的一些观点在我的Android应用程序?


当前回答

如果你的活动与第一个设备朝向状态相关,在onCreate方法中获取当前设备朝向,然后永远修复它:

int deviceRotation = ((WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

if(deviceRotation == Surface.ROTATION_0) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_180)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if(deviceRotation == Surface.ROTATION_90)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(deviceRotation == Surface.ROTATION_270)
{
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}

其他回答

在Kotlin中,同样可以使用下面的代码以编程方式实现:

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

在<apphome>/platform/android目录下,创建AndroidManifest.xml(从生成的目录中复制它)。

然后添加android:screenOrientation="portrait"到所有的活动元素。

你应该在AndroidManifest.xml中修改android:screenOrientation="sensorPortrait

如果你想要用户设置,那么我建议setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

您可以从设置菜单更改设置。

我需要这样做是因为我的计时器必须与屏幕上的内容相对应,旋转屏幕将破坏当前活动。

只需在活动标记中添加此属性。

 android:screenOrientation="portrait"