Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no space between CardViews on Lollipop?

I try to use the CardView and it works well below 5.0, but looks strange on Lollipop.

enter image description here

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card1"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="card2"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</android.support.v7.widget.CardView>
</LinearLayout>

I meet the same question when i use the RecyclerView, should I have to add something if it runs on Lollipop?

like image 233
cajsaiko Avatar asked Nov 20 '14 09:11

cajsaiko


3 Answers

Set this on a CardView:

app:cardUseCompatPadding="true"

From documentation:

Add padding in API v21+ as well to have the same measurements with previous versions.

like image 138
tomrozb Avatar answered Nov 20 '22 22:11

tomrozb


Use this two tags below inside of your cardview:

app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
like image 29
Omar Faroque Anik Avatar answered Nov 20 '22 21:11

Omar Faroque Anik


first image is the expected behavior of card view. when the card has elevation the shadow falls on the bottom layers. In the pre-lollipop devices the elevation is made by adding padding. so the pre-lollipop devices will have a padding around the card view.

Before L, CardView adds padding to its content and draws shadows to that area. This padding amount is equal to maxCardElevation + (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.

like image 13
null pointer Avatar answered Nov 20 '22 20:11

null pointer