34

我在 RecyclerView 中使用 CardView 作为元素。这样做时,android 会自动在 cardView 和屏幕之间以及不同 cardView 之间生成边距。

<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_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/galleryCardBGColor"
app:cardCornerRadius="2dp" >

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />

我按照链接中的说明将它们集成到我的项目中: using-recyclerview-and-cardview-in-eclipse-adt

我之前一直在为列表元素使用线性布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

这工作得很好,列表元素之间没有任何边距。我现在刚刚将线性布局放置在 cardView 中,这导致了额外的边距。

原因是我想为这些元素提供准确的边距,并且我现在提供的任何边距都被添加到这个预先存在的边距中。我尝试为 cardView 元素提供零/负填充/边距,但这些都不起作用。

任何想法,我都可以删除这些边距,或者确切地知道要添加多少边距。

4

11 回答 11

46

它对我有用。利用:

 card_view:cardElevation="0dp"
 card_view:cardMaxElevation="0dp"
于 2015-04-07T10:15:25.533 回答
44

你检查它是边距还是填充?(开发选项/显示布局边界)

CardView在 L 之前的平台中添加填充以绘制阴影。在 L 中,除非您设置useCompatPadding=true,否则不应有任何间隙。

添加负边距(虽然它很难看)应该可以。如果不是,请添加更多代码来说明如何添加它们以及如何设置RecyclerView.

于 2014-11-20T05:18:41.790 回答
18

刚刚在@yigit 的回答中进行了描述,CardView 将添加一个默认填充以在 Android L 之前绘制它的阴影。填充大小在 CardView 的文档中进行了描述。

我发现清除此填充(也为内容添加填充)的方法是使用 CardView 的 contentPaddingLeft(/Right/Top/Bottom) 属性。

如果要清除默认填充,可以将 contentPadding 设置为负值。如果要添加内容填充,请将 contentPadding 设置为所需的值。

就我而言,我使用这些代码来清除默认填充:

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

我尝试了@Shubham 的答案,但是在 RadialGradient.java 中抛出了 IllegalStateException,消息为“半径必须> 0”。

于 2015-04-30T07:07:15.587 回答
15

在下面使用这两个标签:

card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
于 2015-05-21T21:10:28.823 回答
4

添加card_view:cardPreventCornerOverlap="false"到您的card 它仅在您的图像具有圆角时才有效

于 2015-04-07T13:16:58.150 回答
3

我知道它很晚,但我希望它会帮助别人

app:cardPreventCornerOverlap="false"

将 app:cardPreventCornerOverlap 设置为 false 即可:)

于 2017-05-03T09:43:41.437 回答
2

好吧,似乎有一种更简单的方法可以做到这一点,而无需猜测填充和所有内容:

card_view:cardPreventCornerOverlap="false"

或使用java:

cardView.setPreventCornerOverlap(false)
于 2016-05-23T14:56:02.003 回答
1

@vj9 在接受的问题下的评论帮助我计算了偏移量。在这里分享它,因为它可能对其他人有帮助:

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);
} 
于 2016-03-28T10:03:23.710 回答
0

利用

android:adjustViewBounds="true"

在您想要包含在卡片视图中的视图上

于 2017-04-27T12:02:30.170 回答
0

如果您不需要拐角半径或下层高度,请考虑FrameLayout在棒棒糖之前使用。

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

布局/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>

布局-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>
于 2017-08-12T00:24:29.727 回答
-3

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

于 2016-06-16T18:09:40.230 回答