当我在Android评分条中使用自定义星星时,小于0.5的小数值总是显示半颗星。

15

我已经查阅了多篇文章(例如Android RatingBar change star colorsChange 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?),以改变RatingBar中星星的颜色。我按照这些文章的方法成功地为自定义RatingBar更改了星星的颜色,但在这样做的过程中,我无法将值设置为小于半个(例如0.2)。它总是显示为半个(例如0.5)。当我将值设置为大于半个时(例如0.7),它可以完美地显示。

以下是我用于自定义RatingBar的示例图像(即ratingbar_empty.png、ratingbar_half.png、ratingbar_full.png):

Empty Star Half Star Full Star

以下是使用自定义星级的xml(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>'
以下是我如何在我的布局中使用自定义的评分条。
<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" />
当我把所有东西放在一起并将RatingBar的值设为0.2时,它会在视觉上将RatingBar设置为0.5,如下图所示。有人知道这是为什么或者我做错了什么吗?任何帮助都将不胜感激。谢谢。

你尝试过使用更大的步长,比如 0.1 吗? - user
嗨@Luksprog,感谢您的评论。是的,我也尝试过了,但仍然是一样的情况。看到只有在我将值设置为半数以下时才会出现这种奇怪的情况,当我将值设置为半数以上时,它就正常工作了? - Graham Baitson
按照以下步骤将自定义颜色设置为默认评分条: https://dev59.com/tnE95IYBdhLWcg3wHqSl#29853214 - Arshad
1个回答

31

你不需要半颗星。

试着像这样做:

<?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>

感谢您的评论。我最终找到了它,我以为我已经更新了这个。但非常感谢您,我按照您的说法去做了 :-) - Graham Baitson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接