【问题标题】:When I use custom stars for Android RatingBar, always show half star for decimal values below 0.5当我为 Android RatingBar 使用自定义星星时,对于低于 0.5 的十进制值始终显示半星
【发布时间】:2014-01-14 12:40:46
【问题描述】:

我查看了许多帖子(例如Android RatingBar change star colorsChange the color of the stars in the rating bar where the rating bar is created dynamically in androidHow 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)

以下是使用自定义星星的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 的方式。

<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
  • 嗨@Luksprog,感谢您的评论。是的,我也试过了,还是一样。看到这很奇怪,只有当我将值设置为低于一半时才会发生这种情况,当将值设置为高于一半时它工作正常?
  • 按照此将自定义颜色设置为默认评分栏stackoverflow.com/questions/2446270/…

标签: android custom-controls ratingbar


【解决方案1】:

你不需要 star_half。

尝试这样做:

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

【讨论】:

  • 感谢您的评论。我终于找到了它,我以为我更新了它。但是非常感谢,我按照你说的做了:-)
猜你喜欢
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2011-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多