【发布时间】:2020-07-31 23:46:11
【问题描述】:
我有一个 ImageView,我需要在 ImageView 上的特定位置上放置一个 TextView,就像这张图
我已经设法做到了,有点使用以下 xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/imageBg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:textColor="#ff0000"
android:layout_marginRight="200dp"
android:layout_marginTop="205dp"
app:layout_constraintLeft_toLeftOf="@id/background"
app:layout_constraintRight_toRightOf="@id/background"
app:layout_constraintTop_toTopOf="@id/background" />
</androidx.constraintlayout.widget.ConstraintLayout>
这在我的一个模拟器测试设备上运行良好,但正如我在其他模拟器设备上尝试时所期望的那样,由于固定的边距 dp 值,它不能很好地在运行不同分辨率等的多个设备上扩展。
我怎样才能以最好的方式实现这一点,它可以在多个设备上工作,现在 TextView 每次都在同一个位置?我知道我可以使用 dimens.xml 并在那里为不同的设备尺寸使用不同的边距值,但我不认为这会是最好的方法。
【问题讨论】:
标签: android xml android-layout android-constraintlayout