1、布局样式
<RatingBar
android:id="@+id/rbStar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:progressDrawable="@drawable/hybrid_ratingbar_full"
android:rating="2.5" />
2、自定义样式,新建hybrid_ratingbar_full
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@android:id/background"
android:drawable="@mipmap/common_icon_stars_empty" />
<item
android:id="@android:id/secondaryProgress"
android:drawable="@mipmap/common_icon_stars1" />
<item
android:id="@android:id/progress"
android:drawable="@mipmap/common_icon_stars2" />
</layer-list>
3、RatingBar几个重要的属性
android:isIndicator:是否用作指示,用户无法更改,默认false
android:numStars:显示多少个星星,必须为整数
android:rating:默认评分值,必须为浮点数
android:stepSize: 评分每次增加的值,必须为浮点数
默认的样式有如下两种:
style="?android:attr/ratingBarStyleSmall"
style="?android:attr/ratingBarStyleIndicator"
4、事件的监听
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),
Toast.LENGTH_LONG).show();
}
});