我如何支持相同的屏幕尺寸但不同的dpi?

时间:2019-01-04 18:13:36

标签: java android android-studio android-screen-support

所以我在这里有一个图像,并且通过将它们放置在不同的可绘制文件夹中创建了图像的多个副本。

<ImageView
    android:id="@+id/Profile_Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:contentDescription="@string/profile_button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/profile_bar"
    android:focusable="true" />

现在,我的问题是我的应用程序的设计在某些屏幕尺寸上看起来很完美,但是对于其他屏幕而言,看起来并不好。

例如,我的应用程序设计在Nexus 5上看起来很完美,但是当我更改为Pixel时,即使屏幕尺寸相同,它也会弄乱。我了解到dpi有所不同,这就是为什么它搞乱了。

那么,我如何支持具有不同dpi的相同屏幕尺寸?

编辑:链接到Nexus 5和Pixel上的屏幕-https://imgur.com/a/iMeSwV8

编辑2:我决定如何设计三个图像的XML代码。现在的设计是正确的,除了一件事。这三张图像没有出现在屏幕顶部,而是出现在屏幕中央。如果我可以将这些图像移到顶部,我的问题就可以解决。

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

    <ImageView
        android:id="@+id/Profile_Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:contentDescription="@string/profile_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/profile_bar"
        android:focusable="true" />

    <ImageView
        android:id="@+id/Game_Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:contentDescription="@string/game_button"
        app:layout_constraintEnd_toStartOf="@+id/Messages_Button"
        app:layout_constraintStart_toEndOf="@+id/Profile_Button"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/podium_bar"
        android:focusable="true" />

    <ImageView
        android:id="@+id/Messages_Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:contentDescription="@string/messages_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/messages_bar"
        android:focusable="true" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先,如果您提供更多信息(如活动XML代码以及您的期望),将不胜感激。但我提供一些技巧可能会有所帮助。

首先,将具有不同分辨率的不同图像放入可绘制文件夹中会导致加载与手机分辨率匹配的图像。以获得更高分辨率,更高质量的图像,以及更低分辨率,更低尺寸和分辨率的图像。如果您在文件夹中放入1张图片,则android具有自动调整大小功能,可以为手机调​​整图片大小。因此,如果质量不好,在高分辨率下可能会模糊。

在您的问题中,两部手机均加载相同的图像,因为它们具有相同的分辨率。因此问题不是由这些图像引起的。

第二,最重要的是使您的应用在所有纵横比和分辨率上看起来都一样,这是布局的方式。我喜欢LinearLayout,因为它适合各种情况。相互放置一些LinearLayout并花一些时间使它们看起来不错,因为您可以放心,在每种情况下它都很好。

适合您的情况:尝试使用3个子级(imageview)的1个水平LinearLayout。将所有权重都设为1.,则无论每个屏幕大小如何,它们都将以相同大小填充屏幕顶部。

相关问题