问题
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