当TextView高度为wrap content时,如何让图片和文字重叠?

How to Put an image and text overlapped when the TextView height is wrap content?

我正在 Android 工作室项目中工作。这里我使用了一个 imageview 和一个 TextView。我想在图像上放一些文字。 从数据库中获取了不同的文本。文字大小不一。 问题是当文本的大小太长时,它会超过图像的大小,从而超过图像。我给的图片大小是固定的。 TextView 的大小是包装内容。 有什么方法可以使无论文本大小如何,图像大小都会相应匹配? (我可以使用不同的图像以防文本大小变化太大)。 提前谢谢你。

您可以使用相对布局来实现所需的结果。将 imageView alignLeftalignRight 设置为 textView,使其与 textView 的宽度匹配。使用 scaleType = "fitXY" 使图像 src 适合 imageView 的宽度。

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignLeft="@id/text_view"
        android:layout_alignRight="@id/text_view"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text here" />

</RelativeLayout>