RatingBar
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 @Override 15 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 @Override 26 public void onRatingChanged(RatingBar arg0, float arg1, boolean arg2) { 27 // TODO Auto-generated method stub 28 //动态改变图片透明度 29 int ratingInt = (int)(255 / 5 * arg1); 30 imageView.setImageAlpha(ratingInt); 31 } 32 }); 33 } 34 35 @Override 36 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文件
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical" > 5 <ImageView 6 android:id="@+id/imageView" 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:src="@drawable/home" 10 android:layout_gravity="center_horizontal" 11 /> 12 13 <RatingBar 14 android:id="@+id/ratingBar" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:numStars="5" 18 android:max="255" 19 android:progress="255" 20 android:stepSize="0.5" 21 /> 22 </LinearLayout>