使用RecyclerView和卡

Yar*_*lka 12 android android-5.0-lollipop android-recyclerview

我有一些带有卡片物品的RecyclerView.在preLollipop设备上它看起来很好.但是在Lollipop上,它看起来没有儿童卡之间的间隔.我试图使用保证金和填充,但它没有帮助.当我在棒棒糖上使用保证金时,它看起来很好,但是在preLollipop上的间距相同,非常大.再一次:为什么会有不同的间距?我应该使用哪个属性来修复它?

yig*_*git 22

发生这种情况是因为在L上,阴影在View界限之外但是在L之前,它们在视图内部(遗憾的是前L没有真正的阴影支持).

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


Spl*_*tar 15

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

这是我如何工作的一个例子(在yigit的答案中链接的android站点并不明显).

<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>
Run Code Online (Sandbox Code Playgroud)

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

材料设计培训中有一个很好的例子.