效果图:
1.布局
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5" //设置数量
android:rating="3" //设置默认选中
style="@style/mRatingBar"
android:progressTint="@android:color/holo_orange_dark" //设置颜色
android:stepSize="1" /> //设置选中数值
<?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="@drawable/ratingbar_normal" />
<item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ratingbar_normal" />
<item android:id="@android:id/progress" android:drawable="@drawable/ratingbar_checked" />
</layer-list>
3.style
<style name="mRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar</item>
<item name="android:minHeight">38dp</item>
<item name="android:maxHeight">38dp</item> //这里是图片的高度
</style>
RatingBar mRatingBar = (RatingBar) findViewById(R.id.ratingbar);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "评分星级=" + rating, Toast.LENGTH_SHORT).show();
}
});
设置监听,第二个参数就是数值
也可以用对象去get
float rating = mRatingBar.getRating();//获取星级
Demo地址:https://github.com/yechaoa/RatingBarDemo