RatingBar和SeekBar,有相同的父类AbsSeekBar,有的xml属性和progressbar一样。
使用起来很简单,RatingBar几个重要的XML属性如下:
android:isIndicator 设置星级评分条是否允许用户改变
android:numStars 设置星星的数量,一般为5
android:stepSize 设置一次拖动多少个星星,一般为0.5
android:rating 设置默认星级
1.Activity
1 package gdp.ratingbartest; 2 3 import android.annotation.SuppressLint; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.widget.ImageView; 8 import android.widget.RatingBar; 9 import android.widget.RatingBar.OnRatingBarChangeListener;10 11 public class MainActivity extends Activity {12 private ImageView imageView;13 private RatingBar ratingBar;14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.main);18 //获得控件对象19 imageView = (ImageView)findViewById(R.id.imageView);20 ratingBar = (RatingBar)findViewById(R.id.ratingBar);21 //为RatingBar绑定监听器22 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {23 24 @SuppressLint("NewApi")25 @Override26 public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) {27 // TODO Auto-generated method stub28 //动态改变图片透明度29 int ratingInt = (int)(255 / 5 * arg1);30 imageView.setImageAlpha(ratingInt);31 }32 });33 }34 35 @Override36 public boolean onCreateOptionsMenu(Menu menu) {37 // Inflate the menu; this adds items to the action bar if it is present.38 getMenuInflater().inflate(R.menu.main, menu);39 return true;40 }41 42 }
2.xml文件
15 12 13 22