如何在多种屏幕尺寸和密度下使我的布局看起来相同?

him*_*767 1 android android-screen-support

这是我的代码:

<TextView
    android:id="@+id/timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginTop="50dp"
    android:textColor="#873670" />

<TextView
    android:id="@+id/questionList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#110987" />

    <RadioGroup
        android:id="@+id/rgAns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rbOpt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="usrAnsrChoice" />

        <RadioButton
            android:id="@+id/rbOpt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="usrAnsrChoice" />

        <RadioButton
            android:id="@+id/rbOpt3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="usrAnsrChoice" />
    </RadioGroup>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="40dp"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/prevQstn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quizPrvBtnLbl" />

    <Button
        android:id="@+id/endTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quizEndTstBtnLbl" />

    <Button
        android:id="@+id/nextQstn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quizNxtBtnLbl" />

    <TextView
        android:id="@+id/inc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="100dp"
        android:textColor="#ffffff" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我希望这个屏幕能够自动调整为4英寸,5英寸和2英寸屏幕.我还想完全支持mdpi和ldpi屏幕,并且在7英寸屏幕上看起来与在4英寸屏幕上看起来一样.我必须对XML进行哪些更改才能支持这些屏幕大小和密度?

现在我的布局在小屏幕上看起来很好,但在大屏幕上却没有那么多.此外,我不希望我的图像被拉伸或我的文本不对齐.

Mic*_*ley 5

要设计适用于多种不同类型屏幕的布局,您应该为每种屏幕尺寸创建布局文件.这样做的旧方式类似于Danny在他的回答中概述的内容,但不太准确.支持多种屏幕尺寸的旧方法如下:

layout-xlarge:xlarge屏幕至少960dp x 720dp
布局 - 大:大屏幕至少640dp x 480dp
布局 - 正常:普通屏幕至少470dp x 320dp
布局 - 小:小屏幕至少426dp x 320dp

仍然支持这种为不同屏幕尺寸定义布局的方法,但现在建议将该方法替换为最小屏幕宽度.定位屏幕宽度而不是通用尺寸"存储桶"的原因是,在确定屏幕实际适合哪个存储桶并且有些设备报告错误的存储桶时,它有点模棱两可.通过定位屏幕宽度,设备无法再错误地报告其大小.

屏幕宽度文件夹将设置如下:

layout-sw320dp:320dp - >典型的手机屏幕(240x320 ldpi,320x480 mdpi,480x800 hdpi等).
layout-sw480dp:480dp - >像Dell Streak(480x800 mdpi)这样的tweener平板电脑.
layout-sw600dp:600dp - > 7英寸平板电脑(600x1024 mdpi)
.layout-sw720dp:720dp - > 10英寸平板电脑(720x1280 mdpi,800x1280 mdpi等).

对于可绘制资源,您可能希望定位屏幕密度,而不是定位屏幕大小.这样做的基本思想是,您可以创建针对mdpi密度桶的所有资源,并根据不同的密度缩放您的drawable.因此,在这种情况下,您的图像资源有以下可绘制文件夹:

drawable-ldpi:低密度屏幕
drawable-mdpi:普通或中密度屏幕
drawable-hdpi:高密度屏幕
drawable-xhdpi:超高密度屏幕

可以在Android Developer Reference上找到更好的解释,并在此处摘录(由我添加的密度存储桶名称):

替代drawables

几乎每个应用程序都应该有不同的可绘制资源用于不同的屏幕密度,因为几乎每个应用程序都有一个启动器图标,并且该图标应该在所有屏幕密度上都很好看.同样,如果在应用程序中包含其他位图drawable(例如应用程序中的菜单图标或其他图形),则应提供替代版本或每个版本,以适应不同的密度.

注意:您只需要为位图文件(提供特定密度绘项目.png,.jpg.gif)和九路径文件(.9.png).如果使用XML文件来定义形状,颜色或其他可绘制资源,则应将一个副本放在默认的drawable目录(drawable/)中.

要为不同的密度创建替代位图可绘制,您应遵循四个广义密度之间的3:4:6:8缩放比例.例如,如果你有一个位图可绘制的中等密度屏幕的48x48像素(启动器图标的大小),所有不同的大小应该是:

  • 36x36用于低密度(ldpi)
  • 中密度(mdpi)48x48
  • 72x72高密度(hdpi)
  • 额外高密度(xhdpi)为96x96

有关设计图标的更多信息,请参阅图标设计指南,其中包括各种位图可绘制的尺寸信息,例如启动器图标,菜单图标,状态栏图标,选项卡图标等.

有关支持多种屏幕尺寸的完整参考资料可以在Android开发者网站上找到.