1

这是我的代码:

<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>

我希望这个屏幕能够自动调整为 4"、5" 和 2" 屏幕。我还希望完全支持 mdpi 和 ldpi 屏幕,并让 7" 屏幕上的一切看起来与 4" 屏幕上的一样。我必须对我的 XML 进行哪些更改以支持这些屏幕尺寸和密度?

现在我的布局在小屏幕上看起来不错,但在大屏幕上却不是那么好。此外,我不希望我的图像被拉伸或我的文本错位。

4

3 回答 3

5

要设计在几种不同类型的屏幕上运行良好的布局,您应该为每种屏幕尺寸创建布局文件。这样做的旧方法与 Danny 在他的回答中概述的类似,但不太准确。支持多种屏幕尺寸的旧方式如下:

layout-xlarge:xlarge 屏幕至少为 960dp x 720dp
layout-large:大屏幕至少为 640dp x 480dp
layout-normal:普通屏幕至少为 470dp x 320dp
layout-small:小屏幕至少为 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-ldpi:低密度屏幕
drawable-mdpi:普通或中等密度屏幕
drawable-hdpi:高密度屏幕
drawable-xhdpi:非常高密度的屏幕

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

替代绘图

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

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

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

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

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

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

于 2013-03-18T20:51:57.447 回答
0

您应该使用 RelativeLayout 作为根视图。然后您可以指定 UI 元素之间的关系,如上、下等。使用 RelativeLayout 设计多个显示要容易得多

于 2013-03-01T09:51:42.887 回答
-3

用良好的用户界面设计布局。您在其中创建文件夹drawable

1. layout-large for the hdpi devices like google nexus
2. layout-xlarge for the xhdpi devices like touchpads
3. layout-small for the small devices less than medium devices

并根据布局将它们与要设置字体大小的字段和元素对齐。要出现好的用户界面。

于 2013-03-01T10:07:26.273 回答