RatingBar控件的使用

RatingBar控件的使用_第1张图片

在drewable下定义一个图片资源ratingbar_drawable.xml

<?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/travel_hotel_search_star_gary">
    </item>
    <item
        android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/travel_hotel_search_star_gary">
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/travel_hotel_search_star">
    </item>
</layer-list>

之后再styles.xml下添加一个标签,这个style继承父style   Widget.RatingBar

<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar_drawable</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

布局页面

<RatingBar  android:layout_width="wrap_content"  android:layout_height="20dp"  style="@style/roomRatingBar"  android:layout_marginLeft="10dip"  android:id="@+id/ratingBar"  android:rating="1.5"  android:stepSize="0.5"  android:numStars="5"  />

代码

ratingBar= (RatingBar)view.findViewById(R.id.ratingBar);
ratingBar.setRating(4.5f);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        Log.e("星星","等级:" + rating);
        Log.e("星星","星星等级:" + ratingBar.getNumStars());
    }
});

借鉴文章:

Android 中文 API (40) —— RatingBar

Android RatingBar自定义替换系统图片



你可能感兴趣的:(RatingBar控件的使用)