Search code examples
androidandroid-cardview

CardView don't show its background


My CardView can show its background when preview , but when I run my app . Its background can't show.

The follow xml is my layout .

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="14.4dp"
    android:layout_marginRight="14.4dp"
    android:layout_marginTop="14.4dp"
    android:background="@drawable/kp_jy"
    android:orientation="vertical"
    app:cardCornerRadius="7.68dp">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="14dp"
            android:layout_marginLeft="14.4dp"
            android:layout_marginRight="14.4dp"
            android:layout_marginTop="14dp">
            <ImageView
                android:id="@+id/img_icon"
                android:layout_width="38.4dp"
                android:layout_height="38.4dp"
                android:scaleType="centerCrop"
                android:src="@drawable/empty_txxx" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/img_icon"
                android:gravity="center_vertical"
                android:layout_centerInParent="true"
                android:orientation="horizontal">   
                <TextView
                    android:id="@+id/tv_name"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="9.6dp"
                    android:lines="1"
                    android:ellipsize="end"
                    android:textColor="#FF777777"
                    android:textSize="15.36sp" />

                <TextView
                    android:id="@+id/tv_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toLeftOf="@id/tv_unit"
                    android:text="¥99.5"
                    android:textColor="#FF4A4A4A" />
                <TextView
                    android:id="@+id/tv_unit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#FF9B9B9B"
                    android:textSize="11.52sp" />
            </LinearLayout>
        </RelativeLayout>
</android.support.v7.widget.CardView>

The CardView library is 27.1.0 , I have try 27.1.1 and 27.1.0 . but it's not work!

This is not the same question to this , Those code works some days ago . But I have no idea why this happen for five colleague maintain this project and lots of code update those days .

Who has ideas for this problem . Thanks for first!


Solution

  • CardView doesn't accept background from drawable so take a new parent layout inside the CardView and set drawable to it