Why there is no space between CardViews on Lollipop?

陌路散爱 提交于 2019-12-02 14:35:08

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.

Omar Faroque Anik

Use this two tags below inside of your cardview:

app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"

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.

AnV

You have to add app:cardUseCompatPadding="true" to your Cardview. But just adding that may give you an error. To avoid that error, you also have to add xmlns:app="http://schemas.android.com/apk/res-auto" to your CardView.

For example,

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:cardUseCompatPadding="true">

    // Other views here

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

Some would add card_view:cardUseCompatPadding="true" and xmlns:card_view="http://schemas.android.com/apk/res-auto" instead of those mentioned above. Both ways are correct.

If you want to know more about app in XML(Android), please go through this answer :

Although previous answers will solve the problem, they didn't explain what each attribute does. So to be more helpful to answer seekers,

cardPreventCornerOverlap attribute adds padding to CardView on v20 and before to prevent intersections between the Card content and rounded corners.

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

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