[编辑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中运行良好,但是棒棒糖的星形颜色没有变化。
请参阅下面的图片和代码:
Kitkat - 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" />
风格是由任何变化,问题的来源?
答案 0 :(得分: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
更新工作演示:
<强> MainActivity 强>
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"));
}
});
}
}
<强> activity_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>
app gradle:
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'
}
答案 1 :(得分: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)但它仍然可以使用(在Lollipop,Marshmallow中测试)
答案 2 :(得分:0)
使用您要用于RatingBar
的颜色创建ColorStatefinal ColorStateList colorStateList = ColorStateList.valueOf(<int color value>)
然后使用ViewCompat
ViewCompat.setBackgroundTintList(<Your RatingBar>,colorStateList)