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

10

д»ҘдёӢеҰӮдҪ•е®һзҺ°пјҹ

еҰӮжһң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);
        }

请查看此帖子:https://dev59.com/tnE95IYBdhLWcg3wHqSl - NightFury
2个回答

19
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颗星是黄色的,1颗是灰色的。如果是5颗星,则全部都是绿色的。 - Valera Valerianov
如果有1、2或3颗星,它们是红色的,其余为灰色。如果有4颗星,则4颗星是黄色的,另外一颗是灰色的。如果有5颗星,则全部5颗都是绿色的。 - Valera Valerianov
空星总是灰色的!满星 - 根据数量改变颜色。 - Valera Valerianov
setOnRatingBarChangeListener 在您更改评分时触发。 "rating" 值表示总值,因此您可以在 "setOnRatingBarChangeListener" 中更改颜色。 - Murtaza Khursheed Hussain
你可以在这里阅读更多关于 "setOnRatingBarChangeListener" 的内容:http://developer.android.com/reference/android/widget/RatingBar.OnRatingBarChangeListener.html - Murtaza Khursheed Hussain
显示剩余4条评论

0
 android:progressBackgroundTint="#FFF"
    android:progressTint="@color/golden"

将这两行代码保留在RatingBar中,激活的星星颜色将会是金色。适用于Lollipop系统。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接