圆角矩形图像选择器形状在棒棒糖中未正确显示

Rounded rectangle image selector shape is not showing properly in lolliop

我正在尝试将选择器放在矩形图像上,它在 lollipop 上方工作正常,但在 lollipop 和低版本中却不行。

我将 cardview 与两张图片一起使用,一张带有图片选择器,一张实际图片。

任何人都可以指导我为什么它不能正常工作!

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

        <item>
            <shape android:tint="@color/transparent">
                <solid android:color="@android:color/transparent" />
                <stroke
                    android:width="3dp"
                    android:color="@color/black" />

                <corners
                    android:bottomLeftRadius="20dp"
                    android:bottomRightRadius="20dp"
                    android:topLeftRadius="20dp"
                    android:topRightRadius="20dp" />
            </shape>
        </item>
        <item
            android:bottom="2dp"
            android:left="2dp"
            android:right="2dp"
            android:top="2dp">
            <shape android:tint="@color/transparent">
                <solid android:color="@android:color/transparent" />
                <stroke
                    android:width="2dp"
                    android:color="@color/white" />

                <corners
                    android:bottomLeftRadius="20dp"
                    android:bottomRightRadius="20dp"
                    android:topLeftRadius="20dp"
                    android:topRightRadius="20dp" />
            </shape>
        </item>
    </layer-list>





<android.support.v7.widget.CardView
                android:id="@+id/image_selector_item_card_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:cardCornerRadius="20dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:layout_margin="2dp"
                android:clipToPadding="true">
                <ImageView
                    android:id="@+id/image_selector_item_image_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"/>
                <ImageView
                    android:id="@+id/border_selected"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:src="@drawable/image_selector_selected_border"/>
            </android.support.v7.widget.CardView>

预计:

在棒棒糖中:

您可以将其简化为以下经过测试的代码

dummy.xml 内部 drawable

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/white"/>
        <stroke android:color="@color/black" android:width="3dp"/>
        <corners android:radius="20dp"/>
    </shape>
</item>

<android.support.v7.widget.CardView
    android:id="@+id/image_selector_item_card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="true"
    app:cardCornerRadius="20dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/dummy">

        <ImageView
            android:id="@+id/image_selector_item_image_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="6dp"
            android:scaleType="centerCrop" />
    </LinearLayout>
</android.support.v7.widget.CardView>

让我知道它是否有效