【问题标题】:how to change the color RatingBar depending on the number of stars?如何根据星星数量更改颜色 RatingBar?
【发布时间】:2014-12-16 11:38:21
【问题描述】:

以下怎么做?

如果 RatingBar 有 1-3 星 - 红色星。 如果 RatingBar 有 4 颗星 - 黄色星。 如果 RatingBar 有 5 颗星 - 绿色星。

((RatingBar) layout.findViewById(R.id.ratingBar)).setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));


<RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ratingBar" android:stepSize="1" android:clickable="false"
            style="?android:attr/ratingBarStyleSmall" android:layout_gravity="right"
        android:layout_marginRight="15dp" />

编辑:

 @Override
    public View getItemView(int section, int position, View convertView, ViewGroup parent) {
        LinearLayout layout;
        if (convertView == null) {
            LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = (LinearLayout) inflator.inflate(R.layout.item_survey, null);
        } else {
            layout = (LinearLayout) convertView;
        }
        ((TextView) layout.findViewById(R.id.questionSurvey)).setText(surveyBeans.get(section).get(position).getQuestion());
        RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar));
        ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));
        LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
        ((TextView) layout.findViewById(R.id.commentSurvey)).setText(surveyBeans.get(section).get(position).getComment());

        return layout;
    }

回答@Murtaza Hussain 后的工作代码:

RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar));
        ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));
        LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        if (ratingBar.getRating()<=3) {
            stars.getDrawable(2).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
        }
        if(ratingBar.getRating()==4){
            stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        }
        if(ratingBar.getRating()==5){
            stars.getDrawable(2).setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
        }

【问题讨论】:

标签: android android-controls


【解决方案1】:
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.starFullySelected), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.starPartiallySelected), PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.starNotSelected), PorterDuff.Mode.SRC_ATOP);

你可以这样做

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser) {
            // This event is fired when rating is changed   
        }    
    }); 

来自您的代码

 @Override
 public View getItemView(int section, int position, View convertView, ViewGroup parent) {
     LinearLayout layout;
     if (convertView == null) {
         LayoutInflater inflator = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         layout = (LinearLayout) inflator.inflate(R.layout.item_survey, null);
     } else {
         layout = (LinearLayout) convertView;
     }
     ((TextView) layout.findViewById(R.id.questionSurvey)).setText(surveyBeans.get(section).get(position).getQuestion());

     RatingBar ratingBar = ((RatingBar) layout.findViewById(R.id.ratingBar));
     ratingBar.setProgress(Integer.parseInt(surveyBeans.get(section).get(position).getRate()));

     if (ratingBar.getRating() == 2f) {
         //change color of two stars
     } else if (ratingBar.getRating() == 3f) {
         //change color of three stars
     }

     LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
     stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
     stars.getDrawable(1).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
     ((TextView) layout.findViewById(R.id.commentSurvey)).setText(surveyBeans.get(section).get(position).getComment());

     return layout;
 }

【讨论】:

  • 我不需要改变它们的意思。它是一劳永逸的。我只需要显示不同的星星。你写的 - 如果它们是黄色的,则为 3 星。另一个灰色。如果 2 颗星 - 它们是黄色的 - 其他的是灰色的。如果 5 颗星 - 它们都是黄色的。反正我需要。如果 1 或 2 或 3 颗星 - 它们是红色的,其余的 - 灰色。如果 4 颗星 - 4 颗星是黄色和一颗灰色。如果 5 颗星 - 全部 5 - 绿色
  • 没找到你。你想要什么?
  • 如果 1 或 2 或 3 颗星 - 它们是红色的,其余的 - 灰色。如果 4 颗星 - 4 颗星是黄色和一颗灰色。如果 5 颗星 - 全部 5 - 绿色
  • 空星总是灰色的!充满星星 - 根据数量改变颜色
  • setOnRatingBarChangeListener 在您更改评级时触发。 “rating”值表示总计值,因此您可以在“setOnRatingBarChangeListener”中更改颜色
【解决方案2】:
 android:progressBackgroundTint="#FFF"
    android:progressTint="@color/golden"

在 RatingBar 中保留这两条线,活跃的星星颜色将是金色的。为棒棒糖工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2016-02-03
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多