Android CardView doesn't work on Api 21

落花浮王杯 提交于 2019-12-07 18:08:53

问题


I am using android CardView and it works perfectly below Api 21. But when I use it on Api 21 i.e. Lollipop the xml attributes like cardElevation, cornerRadius doesn't 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>

回答1:


I had exactly the same problem with CardView. Setting compatPadding to true fixed the issue.

In your layout try to add the following line to your CardView

card_view:cardUseCompatPadding="true"

Or if you are initializing in code, use

cardView.setUseCompatPadding(true);



回答2:


I came across the same behaviour. I think the shadow is drawn outside the content. So you need space between the content of the view and it's border. I fixed it as follows:

Add margins to your CardView

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

    ...

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


来源:https://stackoverflow.com/questions/27043607/android-cardview-doesnt-work-on-api-21

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!