【问题标题】:How do i place a TextView inside a ImageView on the same place across devices?如何将 TextView 放置在不同设备的同一位置的 ImageView 内?
【发布时间】: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


    【解决方案1】:

    我知道我可以利用 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 个布局,但它是响应式布局 - 您无需为不同的设备创建更多布局。


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

    【讨论】:

    • 我采用了这种方法,因为我认为这是实现这一目标的最灵活的方法。以前从未使用过指南,但它们肯定对此很有帮助!
    【解决方案2】:

    有一个库https://github.com/intuit/sdp 提供了一个新的大小单位 - sdp(可扩展的 dp)。此尺寸单位随屏幕尺寸缩放。它可以帮助Android开发者支持多屏。这是的图片示例

    【讨论】:

    • 看了一下,好像没那么好用。当我尝试将 textview 放置在我的测试模拟器设备上的同一位置时,它的移动足以让它看起来很糟糕。此外,我要求它一直兼容到 SDK 14,这需要至少 om 16
    【解决方案3】:

    如果 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 中的样子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多