【问题标题】:Android layout selection issueAndroid布局选择问题
【发布时间】:2016-11-03 20:08:49
【问题描述】:

我有一个 Activity A,它有 2 种布局。手机具有单一布局,平板电脑具有主从布局。 Master-Detail 布局适用于 7 英寸横向和 10 英寸平板电脑。 我使用了以下布局限定符:

-layout - for phones
-layout-sw720dp - for 10 inch tablet
-layout-w820dp-land - for 7 inch tablet landscape
-layout-h820dp - for 7 inch tablet portrait

到目前为止一切正常。

问题:

现在我希望该活动仅在手机的纵向模式和 7 英寸和 10 英寸平板电脑的两个方向上工作,为此我使用了来自此链接 https://stackoverflow.com/a/14793611/373889 的以下资源和尺寸限定符

for res/values
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <bool name="portrait_only">true</bool>
        </resources>

for res/values-sw720dp
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <bool name="portrait_only">false</bool>
        </resources>

for res/values-w820dp-land
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
                <bool name="portrait_only">false</bool>
            </resources>

for res/values-h820dp
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
                <bool name="portrait_only">false</bool>
            </resources>

现在的问题是,在 7 英寸平板电脑中,如果以纵向模式启动,则在两个方向上都显示正常的手机布局,如果以横向模式启动,则在两个方向上都显示主从布局。它不能正常工作。手机在正常布局下工作正常,而 10 英寸平板电脑在两个方向上都可以正常工作。

如果这种方法有问题,请告诉我。谢谢。

【问题讨论】:

    标签: android xml layout tablet screen-orientation


    【解决方案1】:

    根据我的经验,我认为你应该为每个屏幕尺寸做一个布局,我认为你做到了。在 setonContentView(...) 之后这样做:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

    并且基于屏幕锁定的高度和宽度或不是设备的方向。要锁定,您可以添加这些行:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    

    为了不锁定屏幕,不要在 android manifest 或 Activity 中添加与屏幕方向相关的任何内容,默认情况下启用旋转。

    我希望它有效,让我知道。

    【讨论】:

    • 我实际上已经在 onCreate(); if(getResources().getBoolean(R.bool.portrait_only)){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } 但问题是它在两个方向上都保持相同的布局,而我需要纵向的正常布局和横向的主细节布局。
    猜你喜欢
    • 2012-03-02
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多