android.graphics.drawable.ColorDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow

只谈情不闲聊 提交于 2019-11-30 02:38:53

I get this error when calling setBackgroundColor() method, which is defined in View class.

cardView.setBackgroundColor(Color.parseColor("#ffffcc"));

Instead setCardBackgroundColor() method should be called, which is specific to CardViews and defined within CardView class.

This code setting both background colour and the corner radius works for me:

cardView.setCardBackgroundColor(Color.parseColor("#ffffcc"));
cardView.setRadius(50);

I know this is a old question but, if you wanna change a bgcolor of CardView now in android.support.v7.widget.CardView you can just use setCardBackgroundColor:

 cardView.setCardBackgroundColor(Color.parseColor("#dddddd"));

or just

 cv.setCardBackgroundColor(Color.LTGRAY);
terrigenus

I found that you cannot set the background of a card view. Even in XML. So this is my workaround:

Put everything in a child layout of the card:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:elevation="10dp">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/holder_relative_layout"
    android:orientation="horizontal"
    android:gravity="center_vertical">
...

Then in code:

cardView.row.findViewById(R.id.holder_relative_layout).setBackgroundColor(getResources().getColor(R.color.MediumSeaGreen));
formica

The comment about using SetCardBAckgroundColor with CardViews is correct but I had to cast the Views to View as well to get it to compile

((CardView)holder.itemView).setCardBackgroundColor(Color.parseColor("#FFFFCC"));

All works now.

I faced the same crash. The problem is you are setting background color to CardView programatically in your code.

card.setBackgroundColor(Color.parseColor("#"+color));

comment the line. and set the background and other property in xml itself. Don't do it programatically.

This worked for me. :)

Better you define the different xml file for different situation / data, that you want to differentiate.

Milan Jurkulak

This is an easy problem of support library, if you set background drawable first and then color. They (Google) call getDrawable() to set the background color, if you call color Change. They cast Drawable to RoundRectdrawable and you possibly have another kind of Drawable. For example a ninepatch Drawable. Simply override setBackgroundColor() to setBackgroundDrawable(new RoundedDrawable(...)) Not sure about constructor and class name, just look at source code of cardview.

This is the problem:

@Override
public void setBackgroundColor(CardViewDelegate cardView, int color) {
   ((RoundRectDrawable) (cardView.getBackground())).setColor(color);
}

change to:

@Override
public void setBackgroundColor(int color) {
    setBackgroundDrawable(new RoundRectDrawable(color, getRadius()));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!