CardView inside RecyclerView has extra margins

£可爱£侵袭症+ 提交于 2019-11-27 13:05:22
yigit

did you check if it is margin or padding? (Dev Options / show layout bounds)

CardView adds padding in platforms pre-L to draw shadows. In L, unless you set useCompatPadding=true, there should not be any gap.

Adding negative margins (although it is ugly) should work. If it is not, please add some more code on how you are adding them and how you are setting up the RecyclerView.

It worked for me. Use:

 card_view:cardElevation="0dp"
 card_view:cardMaxElevation="0dp"
Piasy

Just described in @yigit 's answer, the CardView will add a default padding to draw it's shadow before Android L. The padding size is described in CardView's doc.

I found the way to clear this padding (also to add padding for content) is to use CardView's contentPaddingLeft(/Right/Top/Bottom) attributes.

If you want to clear the default padding, you can set the contentPadding to minus value. If you want to add content padding, set the contentPadding to the value you want.

In my case, I use these code to clear the default padding:

card_view:contentPaddingLeft="-3dp"
card_view:contentPaddingRight="-3dp"
card_view:contentPaddingTop="-3dp"
card_view:contentPaddingBottom="-3dp"

I tried @Shubham 's answer, but an IllegalStateException is thrown in RadialGradient.java with message the "radius must be > 0".

Omar Faroque Anik

Use this two tags below:

card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
Shayan_Aryan

add card_view:cardPreventCornerOverlap="false" to your card It works only if your Image has Rounded Corners

Well, seems there is a much easier way to do it, without guessing the padding and all:

card_view:cardPreventCornerOverlap="false"

or using java:

cardView.setPreventCornerOverlap(false)

The comment from @vj9 under the accepted question helped me to calculate the offsets. Sharing it here because it might help someone else:

public static int getVerticalCardViewOffset(Context context) {
    Resources res = context.getResources();
    int elevation = res.getDimensionPixelSize(R.dimen.cardview_default_elevation);
    int radius = res.getDimensionPixelSize(R.dimen.cardview_default_radius);
    return (int) (elevation * 1.5 + (1 - Math.cos(45)) * radius);
}

public static int getHorizontalCardViewOffset(Context context) {
    Resources res = context.getResources();
    int elevation = res.getDimensionPixelSize(R.dimen.cardview_default_elevation);
    int radius = res.getDimensionPixelSize(R.dimen.cardview_default_radius);
    return (int) (elevation + (1 - Math.cos(45)) * radius);
} 
Donbosco Muthiani

use

android:adjustViewBounds="true"

on the view you want wrapped in the cardview

I know its late but i hope it will help someone

app:cardPreventCornerOverlap="false"

setting app:cardPreventCornerOverlap to false will do the trick :)

If you don't need the corner radius or elevation downlevel, consider using FrameLayout on pre-Lollipop.

<include layout="@layout/card_view_compat" />

layout/card_view_compat

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <include layout="@layout/content" />
    </FrameLayout>
</merge>

layout-v21/card_view_compat

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="3dp"
        app:cardElevation="4dp"
        >
        <include layout="@layout/content" />
    </android.support.v7.widget.CardView>
</merge>

myCardView.setShadowPadding(0,0,0,0);

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