【问题标题】:Using RecyclerView with Card使用带有卡片的 RecyclerView
【发布时间】:2015-01-09 14:51:57
【问题描述】:

我有一些带有卡片项目的 RecyclerView。在 preLollipop 设备上看起来不错。但在 Lollipop 上,它看起来在子卡之间没有任何间距。我尝试使用边距和填充,但没有帮助。当我在棒棒糖上使用边距时,它看起来很好,但是在 preLollipop 间距上具有相同的边距时非常大。 再一次:为什么会有不同的间距?我应该使用哪个属性来修复它?

【问题讨论】:

    标签: android android-5.0-lollipop android-recyclerview


    【解决方案1】:

    我遇到了同样的问题。我必须创建一个 layout-v21 文件夹,棒棒糖设备将使用该文件夹,并复制布局,添加我需要正确间隔的边距/填充。

    这样,当您的应用在 lollipop 上启动时,布局将从 layout-v21 文件夹中读取并添加正确的空间,并且任何其他设备将转到您不需要空间的标准布局文件显式设置。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      发生这种情况是因为在 L 上,阴影在 View 边界之外,但 pre-L,它们在 View 内(不幸的是,没有真正的阴影支持 pre-L)。

      如果您希望 CardView 在 L 上也具有相同的间距,请将 cardUseCompatPadding 设置为 true。

      【讨论】:

        【解决方案3】:

        yigit 的回答很有帮助,但没有太多细节,所以我在这里为这篇文章的未来观众添加一些内容。

        这是我如何实现此功能的示例(在 yigit 的答案中链接 android site 并不明显)。

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginLeft="@dimen/activity_vertical_margin"
            android:layout_marginRight="@dimen/activity_vertical_margin"
            android:layout_marginTop="2dip"
            android:layout_marginBottom="2dip"
            card_view:cardUseCompatPadding="true">
        
            // Other layouts and views here
        
        </android.support.v7.widget.CardView>
        

        需要下划线和使用 res-auto 模式的 card_view 并不明显。

        Material Design Training 中有一个很好的例子。

        【讨论】:

        • 您的 CardView 中缺少结束标记。我想修复它,但我需要编辑至少 6 个字符 :((我想不出其他任何改变)-如果你能纠正它会很棒 :)
        • Jepp 看起来不错,感谢您的详细回答 :) !尽管我不得不删除该 card_view:cardUseCompatPadding - 因为它没有得到正确解决。不确定那是因为当时图书馆不见了,但如果它真的不起作用,我会回复你的。虽然我认为它与另一个问题有关,如果你测试它,我猜应该没问题。
        猜你喜欢
        • 1970-01-01
        • 2020-10-04
        • 2014-09-18
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        • 2021-12-29
        相关资源
        最近更新 更多