【问题标题】:Layout for tablets in AndroidAndroid 中平板电脑的布局
【发布时间】:2011-05-10 06:54:54
【问题描述】:

我想在 Android 中为平板电脑和手机创建不同的布局。我应该把布局资源放在哪里才能做出这种区分?

【问题讨论】:

    标签: android android-layout tablet


    【解决方案1】:

    对于布局,我相信您目前只能通过以下方式进行区分:

    res/layout/my_layout.xml            // layout for normal screen size
    res/layout-small/my_layout.xml      // layout for small screen size
    res/layout-large/my_layout.xml      // layout for large screen size
    res/layout-large-land/my_layout.xml // layout for large screen size in landscape mode
    

    您可以找到更多信息,了解可以添加到文件夹结构中以区分不同设置here

    最大的问题是,Android SDK 并没有真正正式整合平板电脑。希望这将在下一个版本的 Android 中得到解决。否则,您只需要确保使用适用于任何屏幕尺寸的缩放布局。

    【讨论】:

    • 我认为问题不在于 SDK。例如,手机的屏幕尺寸为 480x800 hdpi 足以用于平板电脑布局。如果您的应用最适合平板电脑布局,那么在这种情况下,用户无需拥有平板电脑,仍然可以从中受益。因此无需区分平板电脑/手机。您指向的链接完美地解决了这个问题。谢谢。
    • Android 3.2以下支持此功能
    【解决方案2】:

    我知道这是一个老问题,但为了它...... 根据documentation,您应该像这样创建多个资产文件夹

    res/layout/main_activity.xml           # For handsets (smaller than 600dp available width)
    res/layout-sw600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
    res/layout-sw720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)
    

    【讨论】:

    • 我需要为 Nexus 9 和 Nexus 10 设备创建哪些布局文件夹?
    • 尝试将 Nexus 10 内容移动到 sw800dp 的文件夹中。 Nexus 9 将继续从 sw768dp 文件夹中获取线索
    【解决方案3】:

    如果您在代码中使用 Fragment 概念(表示多窗格布局),那么最好使用 wdp 而不是 swdp

    res/layout-w600dp/main_activity.xml   # For 7” tablets (600dp wide and bigger)
    res/layout-w720dp/main_activity.xml   # For 10” tablets (720dp wide and bigger)
    res/layout-w600dp-land/main_activity.xml   # For 7” tablets in landscape (600dp wide and                  bigger)
    res/layout-w720dp-land/main_activity.xml   # For 10” tablets in landscape (720dp wide and bigger)
    

    参考表格了解wdp

    Table 2. New configuration qualifers for screen size (introduced in Android 3.2). 在以下链接中 http://developer.android.com/guide/practices/screens_support.html

    【讨论】:

      【解决方案4】:

      This source 还提供了如何根据设备配置调用任何资源,例如:语言、屏幕宽度/高度、布局方向、屏幕方向...等。

      您必须小心将默认资源设置为提到的来源,例如为平板电脑调用高质量的图标。

      【讨论】:

        【解决方案5】:

        根据文档,您应该像这样创建多个资产文件夹......完整列表......

        res/layout/main_activity.xml  // For handsets (smaller than 600dp available width)
        res/layout/main_activity.xml  // For handsets (smaller than 600dp available width)
        res/layout-sw600dp/main_activity.xml  // For 7” tablets (600dp wide and bigger) 
        res/layout-sw720dp/main_activity.xml  // For 10” tablets (720dp wide and bigger)
        res/layout-sw600dp-land/main_activity.xml  // For 7” tablets in landscape (600dp wide and bigger)
        res/layout-sw720dp-land/main_activity.xml  // For 10” tablets in landscape (720dp wide and bigger)
        

        【讨论】:

          【解决方案6】:

          Android Studio 中的“Orientation for preview”下拉菜单可以帮助生成快速的横向和平板布局 xml。它还为这些布局变化创建单独的文件夹,即 layout-landlayout-sw600dp,并将布局 xmls 放在这些文件夹中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-23
            • 2014-06-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多