如何跨设备将 TextView 放置在 ImageView 的同一位置?

How do i place a TextView inside a ImageView on the same place across devices?

我有一个 ImageView,我需要将 TextView 放置在 ImageView 的特定位置之上,如下图所示

我设法做到了这一点,稍微使用了以下 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 并为不同的设备尺寸使用不同的边距值,但我认为这不是最好的方法。

有图书馆https://github.com/intuit/sdp that provides a new size unit - sdp (scalable dp). This size unit scales with the screen size. It can help Android developers with supporting multiple screens. Here is a picture example of that

我知道我可以利用 dimens.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/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_percent="0.8"
    tools:srcCompat="@tools:sample/backgrounds/scenic" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5"/>


<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.45"/>

<TextView
    android:id="@+id/textView7"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@color/colorAccent"
    app:layout_constraintHeight_percent="0.1"
    app:layout_constraintWidth_percent="0.2"
    android:text="TextView"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintEnd_toStartOf="@+id/guideline3" />

</androidx.constraintlayout.widget.ConstraintLayout>

看起来像这样(黑色箭头是指引线):

现在您只有 1 个布局,但它是一个响应式布局 - 您不需要为不同的设备创建更多布局。


请注意,我的视图没有任何固定尺寸,我使用百分比创建了一个完全响应式布局,有关该主题的更多信息,您可以查看

如果 TextView 在顶部、底部、开始和结束处被限制为图像视图,那么它将位于 ImageView 的中心。然后,您可以使用偏置将 TextView 定位在距离 ImageView 顶部和开始的百分比位置。 TextView 将保持该相对位置,无论屏幕大小或方向如何。

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/background"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="parent"
        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:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="#ff0000"
        app:layout_constraintBottom_toBottomOf="@+id/background"
        app:layout_constraintHorizontal_bias="0.25"
        app:layout_constraintLeft_toLeftOf="@id/background"
        app:layout_constraintRight_toRightOf="@id/background"
        app:layout_constraintTop_toTopOf="@id/background"
        app:layout_constraintVertical_bias="0.25" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是 Android Studio 中的样子。