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

标签 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 进行哪些更改才能支持这些屏幕尺寸和密度?

现在我的布局在小屏幕上看起来不错,但在大屏幕上就不太好了。此外,我不希望我的图像被拉伸(stretch)或我的文本未对齐。

最佳答案

要设计在多种不同类型的屏幕上都能很好地工作的布局,您应该为每种屏幕尺寸创建布局文件。这样做的旧方法类似于丹尼在他的回答中概述的方法,但不太准确。支持多种屏幕尺寸的旧方法如下:

layout-xlarge: xlarge screens are at least 960dp x 720dp
layout-large: large screens are at least 640dp x 480dp
layout-normal: normal screens are at least 470dp x 320dp
layout-small: small screens are at least 426dp x 320dp

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

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

layout-sw320dp: 320dp -> a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc).
layout-sw480dp: 480dp -> a tweener tablet like the Dell Streak (480x800 mdpi).
layout-sw600dp: 600dp -> a 7" tablet (600x1024 mdpi).
layout-sw720dp: 720dp -> a 10" tablet (720x1280 mdpi, 800x1280 mdpi, etc).

对于可绘制资源,您可能希望以屏幕密度为目标,而不是以屏幕尺寸为目标。其基本思想是,您针对 mdpi 密度桶创建所有资源,并针对不同密度缩放可绘制对象。因此,在这种情况下,您的图像资源将具有以下可绘制文件夹:

drawable-ldpi: Low density screens
drawable-mdpi: Normal or medium density screens
drawable-hdpi: High density screens
drawable-xhdpi: Very high density screens

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

Alternative drawables

Almost every application should have alternative drawable resources for different screen densities, because almost every application has a launcher icon and that icon should look good on all screen densities. Likewise, if you include other bitmap drawables in your application (such as for menu icons or other graphics in your application), you should provide alternative versions or each one, for different densities.

Note: You only need to provide density-specific drawables for bitmap files (.png, .jpg, or .gif) and Nine-Path files (.9.png). If you use XML files to define shapes, colors, or other drawable resources, you should put one copy in the default drawable directory (drawable/).

To create alternative bitmap drawables for different densities, you should follow the 3:4:6:8 scaling ratio between the four generalized densities. For example, if you have a bitmap drawable that's 48x48 pixels for medium-density screen (the size for a launcher icon), all the different sizes should be:

  • 36x36 for low-density (ldpi)
  • 48x48 for medium-density (mdpi)
  • 72x72 for high-density (hdpi)
  • 96x96 for extra high-density (xhdpi)

For more information about designing icons, see the Icon Design Guidelines, which includes size information for various bitmap drawables, such as launcher icons, menu icons, status bar icons, tab icons, and more.

可以在 Android Developer Site 上找到有关有效支持多种屏幕尺寸的完整引用。 .

关于android - 如何让我的布局在多种屏幕尺寸和密度下看起来相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15154636/

相关文章:

Android,强制关闭应用程序后到底发生了什么?

android - 如何将可变数量的参数传递给数据绑定(bind)适配器?

java - 解析 : How can I get Relation from a local data store (. ..fromLocalDatastore())?

android - 具有各种屏幕尺寸的自定义评分栏

android - react native 资源问题

android - 您的设备与 Asus Nexus 7 英寸选项卡中的此版本不兼容

Android 使用 FileProvider 拍照

java - if 语句满足后执行 else 语句

android - Nexus 6 和 Nexus 9 屏幕密度

安卓多屏设计