响应式布局设计

Responsive Layout Design

我完成了我的小项目的java代码和功能。最后,我根据设备的大小检查是否支持大量 android 设备。但是失败了。

在研究过程中,我了解到我应该对文本大小使用 sp,对所有其他参数使用 dp。布局 -xml- 通过 spdp 存在。但这不是我想象的那样。

例如,我创建了一个新项目。

我的xml; (在 ConstraintLayout

 <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="72dp"
    android:text="Hello World!"
    android:textSize="105sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="144dp"
    android:text="Check"
    android:textSize="160sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

对于 1080 x 1920 xxhdpi; Click here to see layout

对于 1440 x 2960 hdpi(三星 galaxy s8)Click here to see layout

在galaxy s8中,元素真的很小,视野有问题。我想我误解了一个基本概念。你能帮我理清思路吗?

你在这里遗漏了一些东西:当你设置 android:layout_marginTop="72dp" 时,72dp 将在两个布局文件中以相同的方式解释。

一个解决方案是直接在 xml 文件中使用维度而不是值。

在这里观看:

希望对您有所帮助。

您必须注意屏幕尺寸和像素密度。

处理所有屏幕尺寸的最佳方法是在 LinearLayout 中使用 ConstraintLayoutweightSum(但这会降低 UI 性能)。这将帮助您在所有屏幕上保持元素的相同位置。

像素密度更难处理。例如,对于文本大小,我发现使用不同的 dimens 文件很有用。 右键单击 values 文件夹并单击 Values resource file,输入名称 dimens,然后从左侧选择 Density。在那里你可以 select 你喜欢治疗什么密度。在您制作的每个文件中,您可以制作一个具有相同名称的文本大小,如下所示:

<dimen name="normal_text_size">15sp</dimen>

并且每次设置文本大小时都使用此标签。这样,根据 phone 密度,适当的文本大小将自动 selected.

您可以阅读有关 ConstraintLayout here

了解屏幕尺寸 here

关于像素密度here