android RatingBar自定义图片

今天用到android自带的RatingBar,可是太丑了,由是在网上找了下资料,换成了自定义的图片:

 
首先自己写个style样式:

 <style name="articlelist_item_ratingbar_style" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/articlelist_item_rating_drawable_selector</item>
        <item name="android:numStars">5</item>
        <item name="android:minHeight">@dimen/articlelist_item_rating_height</item>
        <item name="android:maxHeight">@dimen/articlelist_item_rating_height</item>
    </style>

然后把自己定义的图片在drawable文件夹中写一个layer-list:

<?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/articlelist_item_rating_off">
    </item>
    <item
        android:id="@+android:id/secondaryProgress"
        android:drawable="@drawable/articlelist_item_rating_off">
    </item>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/articlelist_item_rating_on">
    </item>

</layer-list>

最后在布局xml中操作:

<RatingBar
                style="@style/articlelist_item_ratingbar_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:rating="2.5"
                 />

这样出来的效果就比原先的要强N多了。


你可能感兴趣的:(android RatingBar自定义图片)