[英]RatingBar - Change stars color based on Rating - Issue with Lollipop
[编辑 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);
}
我正在使用 AppCompatRatingBar 来显示用户选择的评级。 此代码在 KitKat 和 Marshmallow 中运行良好,但在 Lollipop 中星星的颜色不会改变。
请看下面的图片和代码:
奇巧 - 4 星
棒棒糖 - 点击两颗星
棉花糖 - 3星级
代码:
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 时,它会消失。 为了澄清起见,我为 RatingBar 添加了背景颜色。
之前
之后
还没解决!!!
[编辑 2] 添加 AppCompatRatingBar 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" />
风格有什么变化,问题的根源?
在lollipop
工作设置所有状态Empty star
Half star
Full star
评级变化。
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.WHITE); // Empty star
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#757675")); // Partial st
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#757675")); // Full
更新工作演示:
主要活动
package pk.sohail.gallerytest.activity;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.RatingBar;
import pk.sohail.gallerytest.R;
public class MainActivity extends Activity {
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.parseColor("#3C3F41"));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.parseColor("#F7C065"));
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.parseColor("#F7C065"));
}
});
}
}
活动_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white">
<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_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:isIndicator="false"
android:numStars="5"
android:stepSize="1" />
</RelativeLayout>
应用程序等级:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "pk.sohail.gallerytest"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
尝试使用setTint()
直接设置 Tint
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) 但它仍然有效(在 Lollipop、Marshmallow 中测试)
奇怪的是 Lolipop 有这个问题,不知道为什么! 我通过使用它摆脱了这个问题,它适用于 Lolipop 以及所有的 Lolipop 前后。
使用要用于 RatingBar 的颜色创建一个 ColorState
final ColorStateList colorStateList = ColorStateList.valueOf(<int color value>)
然后使用 ViewCompat
ViewCompat.setBackgroundTintList(<Your RatingBar>,colorStateList)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.