How to properly set elevation value to recyclerview?

一个人想着一个人 提交于 2019-12-01 02:37:25
Neanderthal

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!