如何为 320dp 和 360dp 创建布局?

Gan*_*rez 1 android android-emulator android-layout

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

我尝试了所有这些:

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

主文件

<?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

摩托罗拉Atrix

摩托罗拉Atrix

三星 Galaxy SII

三星 Galaxy SII

Foa*_*Guy 5

通常,在设计布局时,您希望避免规划特定的像素大小。如果您想根据需要根据像素将所有布局分开,那么您几乎必须为每个设备提供一个布局(世界上有许多具有不同尺寸屏幕的设备)。通常,您需要为几种不同的尺寸类别提供布局资源。layout-small, layout-normal,layout-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>
Run Code Online (Sandbox Code Playgroud)