android – 如何改变颜色RatingBar取决于星星的数量?

怎么做以下呢?

如果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,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轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...