【问题标题】:How to put text over ImageView如何在 ImageView 上放置文本
【发布时间】:2020-12-20 16:13:32
【问题描述】:

目前我有一个充满图像的网格布局,但我想在它们上显示一些文本,并且文本会随着时间的推移而改变/更新。最好的选择是什么?

XML 代码如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<GridLayout
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:columnCount="3"
    android:rowCount="11"
    android:orientation="horizontal"
    tools:context=".MainActivity2" >

<Button
    android:id="@+id/my_button1"
    android:layout_height="wrap_content"
    android:layout_columnSpan="3"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:layout_marginTop="700dp"
    android:layout_marginBottom="40dp"
    android:text="CLOSE CONNECTION" />

<ImageView
    android:id="@+id/my_img1"
    android:layout_width="120dp"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    android:src="@drawable/ic_svgimg_img"/>

<ImageView
    android:id="@+id/my_img2"
    android:layout_width="120dp"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    android:src="@drawable/ic_svgimg_img"/>
.
.
.

【问题讨论】:

    标签: android android-layout imageview android-imageview android-xml


    【解决方案1】:

    为此,您可以使用FrameLayout。试试这样的

    <FrameLayout>
      <ImageView />
      <TextView />
    </FrameLayout>
    

    FrameLayout 将重叠视图,您可以使用重力来调整位置。如果您需要更复杂的对齐方式,您可以使用更复杂的布局,例如 RelativeLayout 或 ConstraintLayout。

    【讨论】:

      【解决方案2】:

      根据您的设计创建一个包含 TextView 和 ImageView 的自定义布局,并在您的 xml 文件中使用自定义布局,并编写一些以编程方式更新的方法。

      【讨论】:

        【解决方案3】:

        您可以单独创建 gridView 项目,在 gridView 中使用这些项目,并使用 constraintLayout 将文本放在图像上,如下所示:

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        
            <ImageView
                android:id="@+id/button3"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:scaleType="fitXY"
                android:background="@color/colorAccent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHeight_percent="0.2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
            <TextView
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:text="I am the text over the image"
                android:textSize="25sp"
                app:layout_constraintBottom_toBottomOf="@+id/button3"
                app:layout_constraintEnd_toEndOf="@+id/button3"
                app:layout_constraintStart_toStartOf="@+id/button3" />
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        它看起来像这样:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-08-12
          • 2011-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多