如何在高度为 wrap_content 的图像视图上添加文本视图叠加层

How to add a textview overlay on an imageview which has hieght of wrap_content

你好,我有一个问题,我将文本视图添加为图像视图上的叠加层,图像视图的高度为 wrap_content,宽度为 match_parent(我不想更改高度或宽度图像视图的任何成本)问题是文本没有显示

the scaleType i have included to the imageView is fitCenter (From Glide)

这是我的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="5dp"
        android:layout_marginTop="11dp"
        android:layout_marginEnd="5dp"
        app:cardElevation="1dp"
        app:cardMaxElevation="4dp"
        app:shapeAppearanceOverlay="@style/RoundedCorner">

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

            <TextView
                android:id="@+id/rankedNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/circular_background_text"
                android:text="@string/_1"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="40sp"
                android:textStyle="italic" />

            <com.google.android.material.imageview.ShapeableImageView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:contentDescription="@string/todo" />
        </RelativeLayout>
    </com.google.android.material.card.MaterialCardView>
</RelativeLayout>

试试这个,在你的 CardView:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/todo" />

        <TextView
            android:id="@+id/rankedNumber"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/circular_background_text"
            android:text="@string/_1"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="40sp"
            android:textStyle="italic" />
</FrameLayout>