Android CardView not working on Api 21

I use the android CardView

, and it works fine under Api 21. But when I use it to Api 21, that is, Lollipop, the xml attributes, such as cardElevation

, cornerRadius

do not work. What am I doing wrong?

This is my XML layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardElevation="2dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:background="@color/white"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:layout_alignBottom="@+id/live_image">

            <TextView
                android:id="@+id/title_update"
                android:text="@string/msg_error"
                android:layout_width="match_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="18sp"
                android:background="@color/white"
                android:textColor="@color/black"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView_time"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/msg_error"
                    android:layout_weight="0.75"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black_translucent" />

                <TextView
                    android:id="@+id/textViewAlarm"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:visibility="gone"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black"
                    android:textStyle="italic" />
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

      

+3


source to share


2 answers


I had exactly the same problem with CardView

. Installing compatPadding

in true

fixed the problem.

In your layout, try adding the following line to CardView

card_view:cardUseCompatPadding="true"

      



Or if you are initializing in code, use

cardView.setUseCompatPadding(true);

      

+7


source


I encountered the same behavior. I think the shadow is out of the content. Therefore, you need space between the content of the view and the border. I fixed it like this:

Add fields to your CardView



<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_margin="15dp"
    ... >

    ...

</android.support.v7.widget.CardView>

      

0


source







All Articles