【问题标题】:How can I create layout for both 320dp and 360dp?如何为 320dp 和 360dp 创建布局?
【发布时间】:2012-06-21 00:28:49
【问题描述】:

我很失望。我刚刚完成了基于 360dp 的“普通屏幕”项目,但是当我尝试在 Motorola Atrix 中运行时,我感到很惊讶。摩托罗拉 Atrix 是 360dp 而不是 320dp,因为他的宽度是 540px。现在我正在努力寻找要解决的问题。如何创建 360dp 布局?

我尝试了所有这些:

  • res/values-sw360dp
  • res/layout-sw360dp

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#DFEFF1">

    <Button
        android:background="#AAAA11"
        android:id="@+id/button1"
        android:layout_width="@dimen/default_width"
        android:layout_height="@dimen/default_height"
        android:text="SOME TEXT 1"         
        />


    <Button
        android:background="#FFAA11"
        android:id="@+id/button2"
        android:layout_width="@dimen/default_width"
        android:layout_height="@dimen/default_height"
        android:text="SOME TEXT 2"
        android:layout_alignParentRight="true" />
</RelativeLayout>

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="default_width">160dp</dimen>
    <dimen name="default_height">160dp</dimen>

</resources>

摩托罗拉 Atrix

三星 Galaxy SII

【问题讨论】:

  • 您能否发布一些代码,以便我们指导您正确的方向?
  • @Abhijit 我刚刚编辑了我的帖子,并插入了布局和常量。

标签: android android-layout android-emulator


【解决方案1】:

通常在设计布局时,您希望避免规划特定的像素大小。如果您要根据需要根据像素分离所有布局,那么您几乎必须为每台设备提供一个布局(世界上有很多设备具有不同尺寸的屏幕)。通常你会想要为几个不同的大小类别提供布局资源。 layout-smalllayout-normallayout-large 等。如果您提供这些并且您的布局以良好的方式构建,它应该可以很好地扩展到不同尺寸的设备。

当您在较大尺寸的设备上运行布局时,您的布局是否存在特定问题?也许如果您发布我可以帮助您尝试解决它,而无需按像素大小分隔您的布局。

开发者文档中的Supporting Multiple Screens 提供了大量有关如何构建应用程序以便它们能够很好地扩展的重要信息。

编辑:

解决您的问题的一种潜在方法是不要使用静态 dp 值作为宽度,而是让按钮增长以占用(水平)多少空间以填满屏幕的宽度。您可以使用 layout_weight 并将宽度设置为 fill_parent 来做到这一点。我现在无法访问 eclipse,所以我无法测试,但我认为肯定还有一种方法可以在没有线性布局的情况下获得这种效果,但这是我想到的第一种方法。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:background="#DFEFF1">

    <LinearLayout
     android:id="@+id/buttonRow"
     android:orientation="horizontal"
     android:layout_height="@dimen/default_height"
     android:layout_width="fill_parent">


    <Button
        android:background="#AAAA11"
        android:id="@+id/button1"
        android:layout_width="0"
        android:layout_height="@dimen/default_height"
        android:text="SOME TEXT 1"         
        android:layout_weight="1"
        />


    <Button
        android:background="#FFAA11"
        android:id="@+id/button2"
        android:layout_width="0"
        android:layout_height="@dimen/default_height"
        android:text="SOME TEXT 2"
        android:layout_weight="1" />

</LinearLayout>
</RelativeLayout>

【讨论】:

  • 我刚刚编辑了我的原始帖子。我放了两张截图,一张是摩托罗拉 Atrix 的,另一张是三星 Galaxy SII 的。如您所见,两者是不同的。这是 2 个具有不同背景颜色的按钮。
  • 当你使用 layout_weight 时,你应该将 layout_width 设置为 0(我认为如果你使用 fill_parent,lint 会给你一个警告)。但无论如何,这些最终都运行良好。
  • 好电话,我现在确实记得那个警告。应该设置为“0”还是“0dp”?
  • android:layout_weight="1" 是我的第一个想法。
猜你喜欢
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-25
  • 2018-07-03
  • 2018-07-05
  • 1970-01-01
相关资源
最近更新 更多