图片在 Cardview 中时不显示

Image not show when it is in Cardvew

下面是我的代码:

<androidx.cardview.widget.CardView
        android:id="@+id/cv_back"
        android:layout_width="@dimen/walletFuncFrameSize"
        android:layout_height="@dimen/walletFuncFrameSize"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="24dp"
        android:layout_marginStart="24dp"
        app:cardCornerRadius="250dp">

        <androidx.cardview.widget.CardView
            android:layout_width="@dimen/walletFuncSize"
            android:layout_height="@dimen/walletFuncSize"
            app:cardCornerRadius="250dp"
            android:layout_gravity="center">

            <ImageView
                android:layout_width="@dimen/walletFuncImageSize"
                android:layout_height="@dimen/walletFuncImageSize"
                android:background="@drawable/back"
                android:scaleType="centerCrop"
                android:layout_gravity="center" />

        </androidx.cardview.widget.CardView>
    </androidx.cardview.widget.CardView>

当我用我的 Android 10 phone 测试时,它工作正常,没有问题。但是当我用 Android 8.0 phone 测试时,图像不显示。

我试图删除所有的 cardview 但只保留 imageView,并且图像终于显示了。

有谁知道如何解决这个问题?

我也试过:

  1. src和srcCompat以及ImageView的背景
  2. 同时将 androidx.cardview.widget.CardView 更改为 com.google.android.material.card.MaterialCardView
  3. 删除一个 CardView

在卡片视图中放置一个布局,并使图像视图成为它的子视图:

<androidx.cardview.widget.CardView
        android:id="@+id/cv_back"
        android:layout_width="@dimen/walletFuncFrameSize"
        android:layout_height="@dimen/walletFuncFrameSize"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="24dp"
        android:layout_marginStart="24dp"
        app:cardCornerRadius="250dp">

        <LinearLayout
            android:layout_width="@dimen/walletFuncSize"
            android:layout_height="@dimen/walletFuncSize"
            android:background="@drawable/oval"
            android:orientation=vertical
            android:layout_gravity="center">

            <ImageView
                android:layout_width="@dimen/walletFuncImageSize"
                android:layout_height="@dimen/walletFuncImageSize"
                android:background="@drawable/back"
                android:scaleType="centerCrop"
                android:layout_gravity="center" />

        </LinearLayout>
    </androidx.cardview.widget.CardView>

@drawable/oval:

<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/colorPrimary" />
</shape>

尝试从内部卡片视图中删除半径and/or。 如果您看到图像,则需要减小半径或增大卡片视图大小。

ImageView 未显示,因为您的两个卡片视图的半径太大,而卡片视图可能不够大,因此半径与您的图像重叠。

如果要以圆形显示图片

Material 设计库提供 ShapeableImageView 无需任何自定义形状即可创建您想要的任何形状

例如:

<com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/ivSchoolIcon"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:adjustViewBounds="true"
                android:padding="2dp"
                android:scaleType="centerCrop"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:shapeAppearanceOverlay="@style/circleImageView"
                app:srcCompat="@drawable/ic_kid_placeholder" />

style.xml 中定义您的风格:

 <style name="circleImageView" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">50%</item>
        <item name="android:shadowRadius">100</item>
        <item name="android:shadowColor">@color/gray</item>
        <item name="backgroundOverlayColorAlpha">12</item>
    </style>

依赖关系:

implementation 'com.google.android.material:material:1.3.0-alpha01'