我的目标是使用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>
我很感激每个评论,非常感谢您的建议。