How to properly set elevation value to recyclerview?

后端 未结 4 1464
悲哀的现实
悲哀的现实 2021-01-01 18:41

I am working on grid layout using recyclerview in android. The grid occupies a portion of the screen and has a shadow. To get the desired shadow effect I am using an elevati

相关标签:
4条回答
  • 2021-01-01 19:11

    Just set below three property in your recyclerview

    android:outlineProvider="bounds"
    android:background="@null"
    android:elevation="2dp"
    
    0 讨论(0)
  • 2021-01-01 19:19

    The android:elevation does only apply shadows on devices which are running Lollipop or later. If you want to suppot older devices, you have to create a shadow yourself.

    0 讨论(0)
  • 2021-01-01 19:22

    I found the answer after a little bit of searching from here. The problem was the transparent background. Elevation works with only non-transparent backgrounds on views. To fix it we should set android:outlineProvider="bounds" on the view and android:clipToPadding="false" on the view's parent.

    Hope it helps someone.

    0 讨论(0)
  • 2021-01-01 19:30

    For Lollipop and you can use the android:elevation property but below lollipop versions you have to give custom shadow so refer the below code for shadow

    card_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#CABBBBBB"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
    
    <item
        android:left="0dp"
        android:right="0dp"
        android:top="0dp"
        android:bottom="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="2dp" />
        </shape>
    </item>
    </layer-list>
    

    Give this file as a background to your recyclerview inflater file it will work fine.

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