Android 可能会为单向活动使用错误的布局文件

发布于 2024-12-14 14:05:11 字数 767 浏览 1 评论 0原文

我偶然发现,如果您有一个设置为一个方向的 Activity,但包含不同方向的单独布局文件,则当该 Activity 首次启动时,它将使用与设备物理匹配的布局文件进行膨胀。方向,并且不是活动方向匹配的布局文件。因此,如果 AndroidManifest.xml 指定

    <activity
        android:name=".activities.LandscapeGardener"
        android:screenOrientation="sensorLandscape">
        <!-- note for new readers: sensorLandscape is like landscape, but also
             works upside-down.
             Other activity stuff would go here. -->
    </activity>

并且在 layout-port 文件夹中有 LandscapeGardener 的布局,如果 Activity 以纵向加载,则特定于纵向的布局将会膨胀。

顺便说一句,我们遇到这种情况的原因是我们正在开发的 Activity 存在一些特定于纵向的错误,因此我们决定在修复这些错误时抑制用户的纵向模式 - 但当然这个问题使得抑制有些局部!

请注意,如果设备在启动时方向正确,则不会更改为不正确的布局。

I discovered by accident that if you have an Activity that is set to one orientation, but contains a separate layout file for a different orientation, when the Activity is first started it will be inflated with the layout file that matches the device's physical orientation and not the layout file that matches the Activity's orientation. Thus if AndroidManifest.xml specifies

    <activity
        android:name=".activities.LandscapeGardener"
        android:screenOrientation="sensorLandscape">
        <!-- note for new readers: sensorLandscape is like landscape, but also
             works upside-down.
             Other activity stuff would go here. -->
    </activity>

and there is a layout for LandscapeGardener in the layout-port folder, if the Activity is loaded in portrait, the portrait-specific layout will be inflated.

The reason we have this situation, by the way, is that an Activity we're developing has some portrait-specific bugs, so we decided to suppress portrait mode for users while we fix them—but of course this problem makes the suppression somewhat partial!

Note that if the device is correctly oriented at startup, it will not change to the incorrect layout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-12-21 14:05:11

尝试仅横向而不是传感器横向。我很确定它有效。

instead of sensorLandscape, try just landscape. i'm pretty sure it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文