尝试将方向屏幕固定为自然或默认方向时出现问题

Issue when trying to fix orientation screen to natural or default orientation

我想通过在 AndroidManifest 中指定配置来锁定屏幕方向,而不是以编程方式进行。所以我最终采用了以下方法:

values/config.xml

<resources>
    <integer name="orientation">1</integer>
</resources>

values-sw600dp/config.xml

<resources>
    <integer name="orientation">0</integer>
</resources>

如果我以编程方式检查值的资源

getResources().getInteger(R.integer.orientation)

我得到预期值:平板电脑为 0,手机为 1,分别是框架为横向和纵向方向指定的值。

但是如果我在 AndroidManifest 中使用这个资源:

<activity
            android:name="activities.InitialConfigActivity_"
            android:noHistory="true"
            android:screenOrientation="@integer/orientation" />

activity 始终以纵向模式启动,无论是平板电脑还是手机设备。

有什么想法吗?

谢谢!

编辑: 我最初读到你的问题的意思是你想完全控制启动方向。如果您对获得 natural/default 方向感到满意,请按如下所述使用 nosensor


这可能不符合回答的条件——我要分享的内容比评论中的要多。

您可能已经看过这些相关问题中的讨论:related-1, related-2, related-3

根据我的经验,当一个问题已经被许多不同的人调查了四年,但仍未找到解决方案时,通常意味着没有解决方案。

一个选项可能是在您的清单中使用 nosensor 方向:

android:屏幕方向="nosensor"

这会在设备的 "default" 或 "natural" 方向上启动 activity。在我的设备上是:phone=portrait, 7-inch-tablet=portrait, 10-inch-tablet=landscape。如果您想要 7 英寸平板电脑的横向,将不适合您。

This post 包含用于确定设备默认方向的代码。