CardView not showing shadow elevation

北城以北 提交于 2019-11-30 08:42:18

Do not forget that to draw shadow you must use hardwareAccelerated drawing

<application android:hardwareAccelerated="true" ...>

see for details https://developer.android.com/guide/topics/graphics/hardware-accel.html?hl=ru

ZeroOne
<android.support.v7.widget.CardView android:id="@+id/item_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="2dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="2dp"
    app:cardBackgroundColor="@color/md_white_1000"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true">

</android.support.v7.widget.CardView>

app:cardUseCompatPadding="true" is the main catch here

If your manifest file has a android:hardwareAccelerated="false" line, delete it.

The answer is a combination of margin and padding of cardView and its parent.

Try like this:

    <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/minimum_dimem"
            android:layout_marginLeft="@dimen/default_margin"
            android:layout_marginRight="@dimen/default_margin"
            android:layout_marginTop="@dimen/minimum_dimem"
            app:cardBackgroundColor="@color/white"
            app:cardCornerRadius="@dimen/cardview_radius"
            app:cardElevation="@dimen/cardview_default_elevation">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="@dimen/ten_dimem">

                ...

           </RelativeLayout>
</android.support.v7.widget.CardView>

And the result:

There are two things you have missing.

  • First of all, Check your project's AndroidManifest.xml file in the Application tag you have to write

<application android:hardwareAccelerated="true" ...>

instead of

<application android:hardwareAccelerated="false" ...>

  1. the second one is, check the parent tag of your layout file is there written like below line.

  2. if you wanna use app: xmlns. then write : xmlns:app="http://schemas.android.com/apk/res-auto"

  3. if you wanna use card_view: xmlns. then write : xmlns:card_view="http://schemas.android.com/apk/res-auto"

NOTE: you don't need to write both simultaneous. Because it gives you duplicate attribute error.

for more understanding see: https://stackoverflow.com/a/56206240/7832102

The above line is most important when you use the attributes of res-auto.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp" />

</RelativeLayout>

You should have to try this:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>

        <!--Put your other fields between this-->
</android.support.v7.widget.CardView>
AbhayBohra

Try this

    <android.support.v7.widget.CardView
            android:id="@+id/driverCurrentJobsCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dim_8"
            android:background="#ffffff"
            app:cardCornerRadius="4dp"
            card_view:cardElevation="4dp"/>

And make sure that you are using this dependency in gradle

compile 'com.android.support:cardview-v7:23.2.1'

use this inside cardView to show shadow:

app:cardElevation="4dp"
app:cardMaxElevation="4dp"

Try putting this in your rootview:

xmlns:card_view="http://schemas.android.com/apk/res-auto" 

and use this code for elevation:

card_view:cardElevation="10dp"

Add this line in CardView widget

app:cardElevation="5dp"

hope this will help!

     <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="4dp"
    app:cardBackgroundColor="#ffff8800">
     ......
     </android.support.v7.widget.CardView>

Remove the background color first, add the elevation and later add the background again. Worked for me, though I had added the background through the layout designer.

remove below line from AndroidManifest.xml

android:hardwareAccelerated="false"
<android.support.design.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
app:cardCornerRadius="@dimen/_5sdp"
card_view:cardCornerRadius="3dp"
android:layout_margin="@dimen/_3sdp"
card_view:cardElevation="10dp"
card_view:cardUseCompatPadding="true"></android.support.design.card.MaterialCardView>

Use above code and don't forget to use * android:hardwareAccelerated="true"*. It will work definitely.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@android:color/white"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/padding_small"
    android:layout_marginBottom="@dimen/padding_small"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp" >

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