【问题标题】:How to dynamically add text over the image?如何在图像上动态添加文本?
【发布时间】:2021-04-23 04:02:07
【问题描述】:

我必须将文字放在图像上,我想得到以下最终结果:

我列出了我数据库中的所有图像并将它们动态添加到我的 Flexbox 中。 但我需要通过代码动态地做到这一点。

【问题讨论】:

标签: java android image text dynamic


【解决方案1】:

你可以用这个:

<RelativeLayout> 
<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myImageSouce" />

<TextView
    android:id="@+id/myImageViewText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/myImageView"
    android:layout_alignTop="@+id/myImageView"
    android:layout_alignRight="@+id/myImageView"
    android:layout_alignBottom="@+id/myImageView"
    android:layout_margin="1dp"
    android:gravity="center"
    android:text="Default text"
    android:textColor="#000000" />

并使用以下方法动态更改此图像中的文本:

TextView myAwesomeTextView = (TextView)findViewById(R.id.myImageViewText);

myAwesomeTextView.setText("New Text inside Image");

【讨论】:

  • 我通过动态创建 LinearLayout 解决了这个问题:D
【解决方案2】:

您可以使用 Canvas 绘制图像并在画布底部绘制文本。检查Developer Google

【讨论】:

    猜你喜欢
    • 2012-07-15
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 2020-01-18
    • 2014-04-15
    • 2019-03-30
    相关资源
    最近更新 更多