我正在尝试更改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
答案 0 :(得分:3)
DrawableCompat.setTint(ratingBar.getProgressDrawable(),color);
是正确的答案。请接受答案或要求他提供答案并接受
答案 1 :(得分:0)
请参阅https://stackoverflow.com/a/38207513/570168
TL; DR:
layerDrawable = (LayerDrawable) rb.getProgressDrawable().getWrappedDrawable();