Java 带有星星颜色的分级条,取决于星星的数量

Java 带有星星颜色的分级条,取决于星星的数量,java,android,android-layout,android-fragments,ratingbar,Java,Android,Android Layout,Android Fragments,Ratingbar,使用Android RatingBar,我希望收视率星星具有以下颜色(每个收视率的所有星星颜色相同): 一颗星:红色 双星:黄色 三星:蓝色 四星:绿色 五星:深绿色 这是我的代码: RatingBar rateDoctor; rateDoctor = (RatingBar)findViewById(R.id.rateDoctor); rateDoctor.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListen

使用Android RatingBar,我希望收视率星星具有以下颜色(每个收视率的所有星星颜色相同):

  • 一颗星:红色
  • 双星:黄色
  • 三星:蓝色
  • 四星:绿色
  • 五星:深绿色
这是我的代码:

RatingBar rateDoctor;

rateDoctor = (RatingBar)findViewById(R.id.rateDoctor);

rateDoctor.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {


    }
});
和我的XML:

<RatingBar android:id="@+id/rateDoctor" android:layout_marginTop="15dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0" />

试试这个

if(ratingbar.getRating()<=1)
{
LayerDrawable stars = (LayerDrawable) ratingBar
                .getProgressDrawable();
        stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.red),
                PorterDuff.Mode.SRC_ATOP); // for filled stars
        stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.hal_selected),
                PorterDuff.Mode.SRC_ATOP); // for half filled stars
        stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.no_selected),
                PorterDuff.Mode.SRC_ATOP);
}
if(ratingbar.getRating()引用:



请分享您的XML我已经重新制定了您的问题,编辑了标题并添加了您评论中的XML。您应该自己修改并添加一个实际问题,因为目前没有问题!
 <RatingBar
    android:id="@+id/ratingBar2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:progressDrawable="@drawable/custom_ratingbar_selector"
    android:numStars="8"
    android:stepSize="0.2"
    android:rating="3.0" />