简体   繁体   English

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

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

I have an ImageView where I need to place a TextView on top of on a specific location on the ImageView like this drawing我有一个 ImageView 我需要将 TextView 放在 ImageView 上的特定位置的顶部,就像这张图在此处输入图像描述

I have managed to do this, somewhat using the following xml:我设法做到了这一点,有点使用以下 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>

This works fine on my one emulator test device, but as I expected when trying on other emulator devices, it doesn't scale well across multiple devices running different resolutions etc. due to the fixed margin dp values.这在我的一个仿真器测试设备上运行良好,但正如我在其他仿真器设备上尝试时所期望的那样,由于固定的边距 dp 值,它不能在运行不同分辨率等的多个设备上很好地扩展。

How can I achieve this in the best possible way which would work across multiple devices and now that the TextView would be on the same position everytime?我怎样才能以最好的方式实现这一点,它可以跨多个设备工作,现在 TextView 每次都在同一个 position 上? I know that I can make use of dimens.xml and use different margin values for different device dimensions there, but I don't believe this would be the best approach.我知道我可以使用 dimens.xml 并在那里为不同的设备尺寸使用不同的边距值,但我不相信这将是最好的方法。

I know that I can make use of dimens.xml and use different margin values for different device dimensions there, but I don't believe this would be the best approach.我知道我可以使用 dimens.xml 并在那里为不同的设备尺寸使用不同的边距值,但我不相信这将是最好的方法。

You are correct, you can use guidelines like this to make sure that your layout will look the same on different screens:你是对的,你可以使用这样的指南来确保你的布局在不同的屏幕上看起来是一样的:

<?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>

It will look like this (the black arrows are the guidelines):它看起来像这样(黑色箭头是指导方针):

在此处输入图像描述

And now you have only 1 layout but it is a responsive layout - you don't need to create more for different devices.现在您只有 1 个布局,但它是响应式布局 - 您无需为不同的设备创建更多布局。


Please notice that I don't have any fixed sizes on my views, I have created a fully responsive layout using percentage, for more info about the subject you can check my answer .请注意,我的视图没有任何固定大小,我使用百分比创建了一个完全响应的布局,有关该主题的更多信息,您可以查看我的答案

There is a Library https://github.com/intuit/sdp that provides a new size unit - sdp (scalable dp).有一个库https://github.com/intuit/sdp提供了一个新的大小单位 - sdp(可扩展 dp)。 This size unit scales with the screen size.此尺寸单位随屏幕尺寸缩放。 It can help Android developers with supporting multiple screens.它可以帮助Android开发者支持多屏。 Here is a picture example of that这是一个图片示例在此处输入图像描述

If the TextView is constrained to the image view at the top, bottom, start and end, then it will be positioned in the center of the ImageView .如果TextView被限制在顶部、底部、开始和结束的图像视图中,那么它将位于ImageView的中心。 You can then use bias to position the TextView at a percentage position from the top and start of the ImageView .然后,您可以使用 position 和 TextView 的偏差,以 position 的顶部和开头的position百分比 The TextView will maintain that relative position regardless of the screen size or orientation.无论屏幕尺寸或方向如何, TextView都将保持相对 position。

<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>

This is what it looks like in Android Studio.这就是它在 Android Studio 中的样子。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM