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

时间:2013-03-01 09:32:51

标签: 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>

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

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

3 个答案:

答案 0 :(得分:5)

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

  

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

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

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

  

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

对于可绘制资源,您可能希望定位屏幕密度,而不是定位屏幕尺寸。这样做的基本思想是,您可以针对mdpi密度存储桶创建所有资源,并针对不同的密度扩展您的drawable。因此,在这种情况下,您的图像资源有以下可绘制文件夹:

  

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

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

  

替代抽奖

     

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

     

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

     

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

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

有关设计的更多信息   图标,请参阅Icon Design Guidelines,其中包含尺寸信息   用于各种位图绘图,例如启动器图标,菜单图标,   状态栏图标,选项卡图标等。

有关支持多种屏幕尺寸的完整参考资料可在Android Developer Site上找到。

答案 1 :(得分:0)

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

答案 2 :(得分:-3)

使用良好的UI整齐地设计布局。 您在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

并根据布局将字段和元素对齐,并根据布局设置字体大小。 要显示出良好的用户界面。