When I use custom stars for Android RatingBar, always show half star for decimal values below 0.5

StackOverflow https://stackoverflow.com/questions/21113807

質問

I've looked up a number of posts (e.g. Android RatingBar change star colors, Change the color of the stars in the rating bar where the rating bar is created dynamically in android, How can I set the star color of the ratingbar?) in order to change the colours of the stars in the RatingBar. I followed the posts and was able to change the stars for the custom RatingBar, but in doing this I was no longer able to set the value to be a decimal lower than a half (e.g. 0.2). It would always show up as the half (e.g. 0.5). When I set the value to be above the half (e.g. 0.7) it displays perfectly.

The following are the sample images that I am using for my custom RatingBar (i.e. ratingbar_empty.png, ratingbar_half.png, ratingbar_full.png)

Empty Star Half Star Full Star

The following is the xml to use the custom stars (ratingbar_custom.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/ratingbar_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ratingbar_half" />
    <item android:id="@android:id/progress" android:drawable="@drawable/ratingbar_full" />
</layer-list>'

The following is then how I use my custom RatingBar in my layout.

<RatingBar 
    android:id="@+id/rb_rating"
    android:numStars="7"
    android:rating="0"      
    android:stepSize="0.01"                     
    android:isIndicator="true"                                                            
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:progressDrawable="@drawable/ratingbar_custom" />

When I then put it all together and set the value of the RatingBar to be 0.2, it sets the RatingBar visually to 0.5, as shown below. Has anyone any ideas why this is happening or what I'm doing wrong? Any help would be greatly appreciated. Thanks

Setting custom RatingBar to 0.2, but visually showing 0.5

役に立ちましたか?

解決

You don't need star_half.

Try to do like this:

<?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/star_none" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/star_none" />
    <item android:id="@android:id/progress" android:drawable="@drawable/star_fill" />
</layer-list>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top