android - 如何根据星星的数量更改颜色 RatingBar?

标签 android android-controls

如何做到以下几点?

如果 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);
        }

最佳答案

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;
 }

关于android - 如何根据星星的数量更改颜色 RatingBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27503768/

相关文章:

android - 实现类似画廊的 slider 控件

android - 如何在 Android 主屏幕的角落滑动启动应用程序,或者它可能是任何屏幕,如惰性滑动

android - getResourceId 在自定义控件中找不到ID

android - 如何在 native 中更改用户当前位置的蓝点颜色或图像

android - 小米手机默认开启特殊权限

android - 计时器任务不会在android中每秒更新一次值

android - Google 云端硬盘选择帐户对话框不会在后退时关闭

java - Android 中 "IF STATEMENT"的行为非常奇怪

android - 调整 android datepicker 控件的大小