Using RecyclerView with Card

后端 未结 3 1760
醉话见心
醉话见心 2021-02-13 19:01

I have some RecyclerView with Card items. And on preLollipop devices it looks fine. But on Lollipop it looks without any spacing between child cards. I tried to use margin and p

相关标签:
3条回答
  • 2021-02-13 19:23

    I had the same problem. I had to create a layout-v21 folder, which will be used by lollipop devices, and duplicate the layout adding the margin/padding I needed to space it out correctly.

    That way when your app is launched on lollipop the layout will be read in from the layout-v21 folder with the correct space added, and any other device will go to your standard layout file where the space isn't needed to be explicitly set.

    Hope this helps.

    0 讨论(0)
  • 2021-02-13 19:25

    This is happening because on L, shadows are outside the the View bounds but pre-L, they are inside the View (unfortunately there is no real shadow support pre-L).

    If you want CardView to have same spacing on L as well, set cardUseCompatPadding to true.

    0 讨论(0)
  • 2021-02-13 19:40

    yigit's answer was helpful but didn't have much detail so I'm adding some here for future viewers of this post.

    Here is an example of how I got this working (not obvious from linked android site in yigit's answer).

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

    The card_view needing an underscore and to use the res-auto schema wasn't obvious.

    There is a good example of this in the Material Design Training.

    0 讨论(0)
提交回复
热议问题