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

安稳与你 提交于 2019-12-18 14:51:40

问题


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)

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


回答1:


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>


来源:https://stackoverflow.com/questions/21113807/when-i-use-custom-stars-for-android-ratingbar-always-show-half-star-for-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!