CardView not showing Shadow in Android L

前端 未结 17 885
一生所求
一生所求 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 02:58

    In my case the shadow was not showing on Android L devices because I did not add a margin. The problem is that the CardView creates this margin automatically on <5 devices so now I do it like this:

    CardView card = new CardView(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    if (Build.VERSION_CODES.LOLLIPOP == Build.VERSION.SDK_INT) {
        params.setMargins(shadowSize, shadowSize, shadowSize,
                shadowSize);
    } else {
        card.setMaxCardElevation(shadowSize);
    }
    card.setCardElevation(shadowSize);
    card.setLayoutParams(params);
    
    0 讨论(0)
  • 2020-11-28 03:00

    Simply add this tags:

    app:cardElevation="2dp"
    app:cardUseCompatPadding="true"
    

    So its become:

    <android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="?colorPrimary"
        app:cardCornerRadius="3dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        app:contentPadding="1dp" />
    
    0 讨论(0)
  • 2020-11-28 03:01

    You can try by adding this line

     card_view:cardUseCompatPadding="true"
    

    The Whole code will seems like this

      <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:cardUseCompatPadding="true"
            card_view:cardCornerRadius="5dp">
     </android.support.v7.widget.CardView
    
    0 讨论(0)
  • 2020-11-28 03:02

    use app:cardUseCompatPadding="true" inside your cardview. For Example

    <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="@dimen/cardviewMarginRight"
            app:cardBackgroundColor="@color/menudetailsbgcolor"
            app:cardCornerRadius="@dimen/cardCornerRadius"
            app:cardUseCompatPadding="true"
            app:elevation="0dp">
        </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-11-28 03:03

    Add android:hardwareAccelerated="true" in your manifests file like below it works for me

     <activity
            android:name=".activity.MyCardViewActivity"
            android:hardwareAccelerated="true"></activity>
    
    0 讨论(0)
  • 2020-11-28 03:09

    Add this line to CardView....

    card_view:cardUseCompatPadding="true" //for enable shadow
    card_view:cardElevation="9dp" // this for how much shadow you want to show
    

    Tips

    You can avoid layout_marginTop and layout_marginBottom as shadow itself takes some space to the up and down of it.The amount space defined by how much you will use in card_view:cardElevation="ndp" .

    Happy Coding (:

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