如何将带有高亮颜色背景的文本添加到 ImageView

how to add text with highlight color background it to ImageView

我试图将带有高亮颜色的文本添加到 ImageView,我进行了一些搜索并找到了这个 answer 并且我成功应用了它,但是我需要像这样在图片

destination

当前XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/postImage"
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:layout_margin="2dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/postTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/postImage"
        android:layout_alignEnd="@id/postImage"
        android:layout_alignRight="@id/postImage"
        android:layout_alignLeft="@id/postImage"
        android:layout_alignTop="@+id/postImage"
        android:layout_alignBottom="@+id/postImage"
        android:layout_margin="1dp"
        android:ellipsize="end"
        android:gravity="center|bottom"
        android:maxLines="2"
        android:text="Hello"
        android:textColor="@color/white"
        android:textSize="14sp" />
</RelativeLayout>

您可以使用 ConstraintLayout。您还需要创建 drawable 文件

您的布局应如下所示;

<android.support.constraint.ConstraintLayout
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp">

    <ImageView
            android:id="@+id/postImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:src="@android:drawable/stat_sys_headset"/>


    <TextView
            android:id="@+id/postTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@drawable/border_bottom_rounded"
            android:paddingBottom="2dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:text="title"/>


</android.support.constraint.ConstraintLayout>

您的可绘制文件 (border_bottom_rounded) 应该是这样的;

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#A9000000"/>
    <stroke android:width="1dp" android:color="#A9000000" />
    <corners android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

简单的方法是:为文本视图添加背景颜色并根据透明度设置 alpha 值。

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

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_image"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="lasdfjsdfjlsdkfj"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        android:layout_alignBottom="@id/image"
        android:layout_alignLeft="@id/image"
        android:textSize="18sp"
        android:layout_alignRight="@id/image"
        android:alpha="0.5"/>

</RelativeLayout>

您可以使用@Ergin Ersoy 和@Ravi 建议的两个选项

创建可绘制文件后(border_bottom_rounded)

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#A9000000"/>
    <stroke android:width="1dp" android:color="#A9000000" />
    <corners android:bottomLeftRadius="5dp"
    android:bottomRightRadius="5dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

将其设置为 TextView 的背景,我还建议将 android:alpha="1" 设置为 0.5 以使文本清晰

完整结果xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/postImage"
        android:layout_width="match_parent"
        android:layout_height="125dp"
        android:layout_margin="2dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/postTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/postImage"
        android:layout_alignLeft="@id/postImage"
        android:layout_alignEnd="@id/postImage"
        android:layout_alignRight="@id/postImage"
        android:layout_alignBottom="@+id/postImage"
        android:layout_margin="1dp"
        android:alpha="1"
        android:background="@drawable/border_bottom_rounded"
        android:ellipsize="end"
        android:gravity="center|bottom"
        android:maxLines="2"
        android:text="Hello"
        android:textColor="@color/white"
        android:textSize="14sp" />
</RelativeLayout>