【问题标题】:Supporting multiple screens in android application在android应用程序中支持多个屏幕
【发布时间】:2014-05-10 05:18:41
【问题描述】:

在开发 Android 应用时支持多屏幕时遇到问题。

首先,我用文件activity_main.xml 做了一些布局设计。 我从来没有用过AbsoluteLayout,只用过LinearLayout。 另外,我使用了一些布局 fill_parent、match_parent、wrap_content 和 dp。 (大概是用 dp 有问题吧?)

其次,我创建了文件夹,并在每个文件夹中复制了 activity_main.xml 文件。

res/layout/activity_main.xml

res/layout-small/activity_main.xml

res/layout-large/activity_main.xml

res/layout-xlarge/activity_main.xml

res/layout-xlarge-land/activity_main.xml

最后,我在清单文件中添加了这些代码。

<compatible-screens>
        <screen android:screenSize="small" android:screenDensity="ldpi" />
        <screen android:screenSize="small" android:screenDensity="mdpi" />
        <screen android:screenSize="small" android:screenDensity="hdpi" />
        <screen android:screenSize="small" android:screenDensity="xhdpi" />

        <screen android:screenSize="normal" android:screenDensity="ldpi" />
        <screen android:screenSize="normal" android:screenDensity="mdpi" />
        <screen android:screenSize="normal" android:screenDensity="hdpi" />
        <screen android:screenSize="normal" android:screenDensity="xhdpi" />

        <screen android:screenSize="large" android:screenDensity="ldpi" />
        <screen android:screenSize="large" android:screenDensity="mdpi" />
        <screen android:screenSize="large" android:screenDensity="hdpi" />
        <screen android:screenSize="large" android:screenDensity="xhdpi" />

        <screen android:screenSize="xlarge" android:screenDensity="ldpi" />
        <screen android:screenSize="xlarge" android:screenDensity="mdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="hdpi" />
        <screen android:screenSize="xlarge" android:screenDensity="xhdpi" />
    </compatible-screens>

我在韩国买的三星 Galaxy Note 上测试过,布局不适合整个屏幕。

谁能帮我解决这个问题?

【问题讨论】:

  • 下载 google io schedule 应用,看看他们是如何设计布局的。
  • 检查并发布您的设备设置的密度为 Log.v(TAG, "density=" + getResources().getDisplayMetrics().densityDpi);
  • @Raghunandan 好吧,这并不意味着我可以看到开发者的代码。
  • @user3578570 您还可以获取源代码,看看他们如何设计适用于所有设备的布局
  • @SaqibVohra 感谢您的评论。好吧,我正在寻找一些我也可以适用于其他设备的代码。不只是我的。你知道怎么做吗?

标签: android xml android-layout layout android-fullscreen


【解决方案1】:

你在这里做的太多了!!

您不需要在不同文件夹中复制布局,如果它们每次都完全相同的话。

从头开始。删除清单中的整个 compatible-screens 部分。

从一个没有限定符的布局开始,并且仅在您尝试解决特定问题(您无法通过调整初始布局来解决)时创建新布局。

您似乎只在一部手机(旧 Galaxy Note 1)上测试了此布局,甚至还没有尝试在多部手机上进行测试。通常,先让它在一个屏幕上运行,然后再尝试让它在多个屏幕上运行。

可以使用 dps 代替 wrap_content 或 fill_parent 等相对值,只需尝试在填充、边距和 minHeight/minWidth 中使用 dps,然后再尝试在 Width 和 Height 中使用它们。并尝试使用重力、重量或相对布局,以尽量减少使用 dps 对过多属性进行硬编码。

另外,请尝试使用九个补丁 (.9.PNG) 而不是直接的 PNG。

最终,您会想学习使用布局设计器预览所有屏幕功能(Android Studio 中的功能,但现在 Eclipse 也具有相同功能)。

如果您想在布局中同时支持平板电脑和手机,请务必尝试使用片段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多