Android 自定义控件评星RatingBar工具类

时间:2022-08-31 00:11:30
public class RatingBar extends LinearLayout {
   
   public RatingBar(Context context, AttributeSet attrs) {
      super(context, attrs);
   }
   
   @Override
   protected void onFinishInflate() {
      super.onFinishInflate();
   }
   
   /**  * max0  */  public void setRating(int count){
        //获得子控件的总个数  int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            ImageView iv = (ImageView) getChildAt(i);
            if(i<count) {
                iv.setImageResource(R.drawable.start_selected);
            }else {
                iv.setImageResource(R.drawable.start_normal);
            }
      }
   }

}
 
//注意布局倒包不要倒错   R.drawable.start_selected图片自己找