CardView not showing Shadow in Android L

前端 未结 17 869
一生所求
一生所求 2020-11-28 02:50

My Cardview inside Listview is not showing shadow in Android L(Nexus 5). Also the round edges are not properly shown. Here is the code for Listview\'s Adapter View :

相关标签:
17条回答
  • 2020-11-28 03:09

    First of all make sure that following dependencies are proper added and compiled in build.gradle file

        dependencies {
        ...
        compile 'com.android.support:cardview-v7:21.0.+'
    
        }
    

    and after this try the following code :

         <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_width="match_parent"
                android:layout_height="200dp"
                android:layout_margin="5dp"
                android:orientation="horizontal"
                card_view:cardCornerRadius="5dp">
         </android.support.v7.widget.CardView
    

    problem rises in Android L beacuse layout_margin attribute is not added

    0 讨论(0)
  • Card view can't show shadow because of your RelativeLayout is on the card view's shadow. To show the shadow add margins on your Card view. For example:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="8dp">
    
    </android.support.v7.widget.CardView>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-28 03:10

    Finally i was able to get shadows on Lollipop device by adding margin to the cardview. Here is the final cardview layout :

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
    <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" >
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >
    
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="@dimen/padding_small"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <ImageView
            android:id="@+id/ivPicture"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvName"
            android:layout_centerHorizontal="true"
            android:scaleType="fitCenter" />
    
        <TextView
            android:id="@+id/tvDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/ivPicture"
            android:layout_centerHorizontal="true"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin" />
    </RelativeLayout>
    

    0 讨论(0)
  • 2020-11-28 03:11

    After going through the docs again, I finally found the solution.

    Just add card_view:cardUseCompatPadding="true" to your CardView and shadows will appear on Lollipop devices.

    What happens is, the content area in a CardView take different sizes on pre-lollipop and lollipop devices. So in lollipop devices the shadow is actually covered by the card so its not visible. By adding this attribute the content area remains the same across all devices and the shadow becomes visible.

    My xml code is like :

    <android.support.v7.widget.CardView
        android:id="@+id/media_card_view"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true"
        >
    ...
    </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-11-28 03:14

    You can add this line of code for shadow in card view

    card_view:cardElevation="3dp"
    

    Below you have an example

    <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"
        card_view:cardElevation="3dp"
        card_view:cardCornerRadius="4dp">
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-28 03:14

    Check you're not using android:layerType="software" in your RecyclerView.

    Changing it to android:layerType="hardware" solved the issue for me.

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