CardView not showing Shadow in Android L

前端 未结 17 960
一生所求
一生所求 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);
    

提交回复
热议问题