RatingBar changing star color without using custom images

谁说我不能喝 提交于 2021-02-07 04:26:54

问题


Is there any way to change the color of star?
I don't want to use custom images to achieve it.


回答1:


You can add these lines into on create method.

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(0).setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);



回答2:


I don't think there is a way. Every Android version defines the default style for the rating bar and the default style uses images too. So the only thing you can do is to use your own images instead.




回答3:


private void setRatingStarColor(Drawable drawable, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DrawableCompat.setTint(drawable, color);
        } else {
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
    }


来源:https://stackoverflow.com/questions/20209884/ratingbar-changing-star-color-without-using-custom-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!