RecyclerView 中的 CardView 有额外的边距 [英] CardView inside RecyclerView has extra margins

查看:35
本文介绍了RecyclerView 中的 CardView 有额外的边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

任何想法,我都可以删除这些边距或以其他方式确切知道添加了多少边距.

解决方案

你检查过是margin还是padding?(开发选项/显示布局边界)

CardView 在平台 pre-L 中添加填充以绘制阴影.在L中,除非你设置了useCompatPadding=true,否则不应该有任何差距.

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

I am using a CardView as element inside a RecyclerView. When doing so android automatically generates margins between the cardView and the screen and between different cardViews.

<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" />

I followed the instructions in the link to integrate them into my project: using-recyclerview-and-cardview-in-eclipse-adt

I had been previously using a linearlayout for the list element :

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

This was working perfectly fine, with no margins whatsoever between the list elements. I have now just placed the linear layout inside the cardView which has resulted in extra margins.

The reason being that i want to provide exact margins to these elements, and any margins I supply now is being added over to this preexisting margins. I have tried supplying zero/negative paddings/margins to the cardView element but none of these work.

Any idea, I can remove these margins or otherwise know exactly how much margin is being added.

解决方案

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.

这篇关于RecyclerView 中的 CardView 有额外的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆