[编辑3]-现在工作
根据@sjy建议,更改了代码如下:
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar)); // Partial star
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
layerDrawable.getDrawable(1).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
layerDrawable.getDrawable(2).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
layerDrawable.getDrawable(0).setTint(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));
} else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP);
}
我正在使用AppCompatingBar显示用户选择的分级。这个代码在kitkat和marshmallow中运行良好,但是在棒棒糖中星星的颜色不会改变。
请看下面的图片和代码:
北极光-4星
棒棒糖-点击两颗星星
棉花糖-三星
代码:
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
if ((int) rating == 1) {
ratingBar.setNumStars(5);
ratingBar.setProgress(1);
ratingBar.setRating(1);
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar));
}else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorOneStar), PorterDuff.Mode.SRC_ATOP);
}
textViewRating.setText(getResources().getString(R.string.worst));
} else if ((int) rating == 2) {
ratingBar.setNumStars(5);
ratingBar.setProgress(2);
ratingBar.setRating(2);
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars));
}else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorTwoStars), PorterDuff.Mode.SRC_ATOP);
}
textViewRating.setText(getResources().getString(R.string.poor));
} else if ((int) rating == 3) {
ratingBar.setNumStars(5);
ratingBar.setProgress(3);
ratingBar.setRating(3);
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars));
}else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorThreeStars), PorterDuff.Mode.SRC_ATOP);
}
textViewRating.setText(Html.fromHtml(getResources().getString(R.string.onetime_watch_one_line)));
} else if ((int) rating == 4) {
ratingBar.setNumStars(5);
ratingBar.setProgress(4);
ratingBar.setRating(4);
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars));
}else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFourStars), PorterDuff.Mode.SRC_ATOP);
}
textViewRating.setText(getResources().getString(R.string.good));
} else if ((int) rating == 5) {
ratingBar.setNumStars(5);
ratingBar.setProgress(5);
ratingBar.setRating(5);
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars));
}else {
layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorFiveStars), PorterDuff.Mode.SRC_ATOP);
}
textViewRating.setText(getResources().getString(R.string.excellent));
}
}
});
[编辑1]
根据@sohail的建议,我补充道
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), ContextCompat.getColor(PostMovieReviewActivity.this, R.color.colorPrimary));
当点击评级栏时,它消失了。为了澄清这一点,我在ratingbar中添加了一个背景色。
以前
之后
还没有决定!!!
[编辑2]
添加AppCompatingBar XML(如果有帮助):
<android.support.v7.widget.AppCompatRatingBar
android:id="@+id/ratingBar"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:background="@color/colorFiveStars"
android:isIndicator="false"
android:max="5"
android:numStars="5"
android:stepSize="1" />
风格有什么变化吗?问题的根源?
最佳答案
尝试使用setTint()
直接设置色调
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
layerDrawable.getDrawable(1).setTint(getResources().getColor(R.color.colorOneStar));
layerDrawable.getDrawable(2).setTint(getResources().getColor(R.color.colorOneStar));
}
注意:getcolor(int)已被弃用,但它仍然有效(在棒棒糖、棉花糖中测试)
关于android - RatingBar-根据评分更改星星颜色- Lollipop 问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39093432/