【问题标题】:How to scale UI elements for different screen sizes in Android using ConstraintLayout如何使用 ConstraintLayout 在 Android 中针对不同屏幕尺寸缩放 UI 元素
【发布时间】:2020-12-30 08:53:04
【问题描述】:

我的目标是在 Android 中使用 ConstraintLayout 设计一个布局,以适应不同的屏幕尺寸,从而适应不同的设备。我经常读到你应该使用 dp 和 sp 来设计布局。基本上我认为使用 dp 和 sp 不会使布局针对不同的屏幕尺寸进行扩展,所以我不同意这一点。在这里,您可以看到设计好的布局在 5' 和 7' 屏幕上的外观。

您可以清楚地看到(屏幕截图具有相同的缩放级别)在 7' 上显示的按钮和图像视图(其高度和宽度由 dp 指定)和文本视图(其大小由 sp 指定)看起来更小或相同大小就像在 5' 设备中一样。但是在 7' 显示器上,元素应该更大并且可以缩放到我认为使用 dp 和 sp 无法实现的显示尺寸。

所以我的问题是如何设计布局以使其缩放到当前的屏幕尺寸,这意味着元素在小型设备中应该更小,而在大屏幕设备中应该更大。

这里可以看到 XML 布局文件:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="265dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        tools:ignore="ContentDescription" />







    <ImageButton
        android:id="@+id/commentButton"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="@null"
        android:contentDescription="comment_Button"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.82"
        app:srcCompat="@mipmap/comment" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/comment"
        android:textSize="18dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.025"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.753" />



    <Button
        android:id="@+id/button"
        android:layout_width="163dp"
        android:layout_height="72dp"
        android:background="@drawable/custom_button"
        android:text="Button"
        android:textAllCaps="false"
        android:textColor="#121212"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.867" />


    <TextView
        android:id="@+id/textViewS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Textview"
        android:textColor="#000000"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.012"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.581" />

</androidx.constraintlayout.widget.ConstraintLayout>

感谢您的每一条评论,并非常感谢您的建议。

【问题讨论】:

  • dp 用于显示具有相同大小的视图,而与像素密度无关。要相对于屏幕尺寸缩放视图,您必须为不同的屏幕尺寸创建不同的布局文件并在其中使用 dp。
  • 感谢 Tayyab 的评论。考虑到存在许多不同的屏幕尺寸,为不同的屏幕尺寸创建不同的布局文件是一项巨大的工作。在 andorid Studio 的布局编辑器中,表格和智能手机有 11 种不同的屏幕尺寸。我有大约 20 个布局文件,所以这意味着我必须为一个应用程序创建 11*20=220 个不同的布局文件。没有更有效的方法吗?我假设我不是唯一遇到此类问题的人
  • 你知道这在Android应用的专业开发中通常是怎么做的吗?在为多种屏幕尺寸开发应用程序时,推荐的方法是什么?最佳实践是什么?对我(作为一个初学者)来说,我无法想象你必须为一个应用程序开发数百种布局配置(但我可能在这个上错了)
  • @TayyabMazhar:我最后一个 cmets 上的任何 cmets。我非常感谢您提供任何进一步的 cmets。
  • 我没有发表评论,因为有人已经提到了一个解决方案,其中(与我的不同)您不必创建多个布局文件。

标签: android scale android-constraintlayout


【解决方案1】:

我的应用程序有不同的维度资源,为更小的屏幕缩放所有 dp 和 sp 值。例如res/values/dimens.xml

<dimen name="dp20">20dp</dimen>
<dimen name="dp24">24dp</dimen>
<dimen name="dp28">28dp</dimen>
<dimen name="dp30">30dp</dimen>
<dimen name="dp32">32dp</dimen>
<dimen name="dp36">36dp</dimen>

res/values-w320dp/dimens.xml:

<dimen name="dp16">12dp</dimen>
<dimen name="dp18">14dp</dimen>
<dimen name="dp20">15dp</dimen>
<dimen name="dp24">18dp</dimen>
<dimen name="dp28">21dp</dimen>
<dimen name="dp30">23dp</dimen>
<dimen name="dp32">24dp</dimen>
<dimen name="dp36">27dp</dimen>

然后,当使用@dimen/dp16 而不是16dp 设置尺寸时,屏幕的缩放效果会更好。您可以为更多类型的屏幕添加更多合格资源。

【讨论】:

  • 感谢 Dominik 的回答。您如何计算“转换后的”dp 值?你的方法是专业 Android 开发中常用的吗?
  • 我刚刚使用“直觉”来计算比例,在本例中为 75% - 我已经在较大的设备上绘制了一些视图,并在较小的设备上测试了缩放,直到获得所需的结果。我不知道它在专业发展中的应用有多普遍。
  • 感谢您的回答和努力 Dominik(我赞成)。你知道这样做的推荐方法是什么吗?
  • 感谢您的支持 :) 我不知道任何推荐的方式,官方 Android 文档仅提到创建其他布局或使用带百分比值的 ConstraintLayout,以便尺寸可以更好地适应其他纵横比。
  • 是的,我的方法与上述库中的 sdp 和 ssp 基本相同。但是,我不知道它的存在。
【解决方案2】:

有一个第三方库可以轻松管理您的问题。

检查此组件的不同大小:https://github.com/intuit/sdp

这是针对文本大小的:https://github.com/intuit/ssp

【讨论】:

  • 感谢 Mehul 的回答(我赞成)。你知道这通常是如何在 Android 应用程序的专业开发中完成的吗?在开发适用于多种屏幕尺寸的应用时,推荐的方法是什么?最佳实践是什么?
  • 基本上我不想使用任何第三方库。 Android 中是否还有一个原生官方元素可以做到这一点?
  • 我在每个应用程序中都使用我与您共享的内容。 @VanessaF
  • 如果您不想使用第三方,那么您必须为不同的分辨率添加不同的尺寸文件。 @VanessaF
  • 感谢 Mehul 的评论。但这是官方推荐的方式还是你知道你的方法是否也用于专业编程?
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 2021-07-15
  • 2013-09-01
  • 1970-01-01
相关资源
最近更新 更多