我正在尝试更改RatingBar星星的颜色,但是在迁移到Android API 23之后,我的以下代码不起作用,并给了我一个例外。

LayerDrawable ratingBarStars = (LayerDrawable) holder.ratingBar.getProgressDrawable();
        ratingBarStars.getDrawable(0).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);
        ratingBarStars.getDrawable(1).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);
        ratingBarStars.getDrawable(2).setColorFilter(mContext.getResources()
                                                             .getColor(R.color.accent), PorterDuff.Mode.SRC_ATOP);


错误是:

java.lang.ClassCastException: android.support.v4.graphics.drawable.DrawableWrapperHoneycomb cannot be cast to android.graphics.drawable.LayerDrawable

最佳答案

Selvin在评论部分中的DrawableCompat.setTint(ratingBar.getProgressDrawable(),color);是正确的答案。请接受答案或要求他提供答案并接受

08-16 04:29