自定义评分条始终显示五颗星

3
我正在开发一款Android应用程序,使用自定义的评分栏。我按照教程(http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/)进行操作,但是当我使用自定义样式时,它总是显示5颗星,即使我将RatingBar强制设置为某个值(比如3颗星)。我在网上查看了其他一些教程,但没有改变任何东西...
我的文件: custom_ratingbar_full.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/background"
        android:drawable="@drawable/custom_ratingbar_full_empty" />
    <item android:id="@+id/secondaryProgress"
        android:drawable="@drawable/custom_ratingbar_full_empty" />
    <item android:id="@+id/progress"
        android:drawable="@drawable/custom_ratingbar_full_filled" />
</layer-list>

custom_ratingbar_full_empty.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_off" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_off" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_off" />

    <item android:drawable="@drawable/star_off" />

</selector>

custom_ratingbar_full_filled.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_full" />

    <item android:state_focused="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_full" />

    <item android:state_selected="true"
        android:state_window_focused="true"
        android:drawable="@drawable/star_full" />

    <item android:drawable="@drawable/star_full" />

</selector>

styles.xml

<style name="CustomRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/custom_ratingbar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

my_fragment.xml

...

        <RatingBar
            android:id="@+id/rating_bar"
            style="@style/CustomRatingBar"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:isIndicator="true"
            android:layout_marginBottom="10dp"
            android:visibility="invisible" />

...

任何解释吗?我已经搜索了我做错了什么,但是我找不到它...
编辑:
如何设置我的评分值:
我在片段之间发送一个捆绑包。
myRating = bundle.getInt("rating");

我检索RatingBar:

RatingBar ratingBar = (RatingBar) rootView.findViewById(R.id.rating_beer);

最后,我测试该值以显示或隐藏评分栏:
if (beerRating != 0) {
    ratingBar.setRating(myRating);
    ratingBar.setVisibility(View.VISIBLE);
    Toast.makeText(getActivity(), "Rating : " + myRating, Toast.LENGTH_LONG).show();
}

我已经验证了我的价值,但它没有显示正确的星数。但是如果我从评分栏中删除自定义样式,它就可以正常工作。

编辑2解决方案:

最终我找到了解决方案:我用@+id替换了@+android:id标签,这就是为什么它不起作用的原因。


你能发布一下给评分条赋值的代码吗? - Heshan Sandeepa
1
我已经更新了我的帖子。 - Azuken
@Azuken在教程中提到没有"@+android:id",我也遇到了同样的问题,但是找不到解决方法。您是如何解决的? - jean d'arme
在教程中,标签被写成 @+android:id,我的 IDE 显示为错误,所以我进行了替换。但是你必须按照教程上的写法来编写才能使你的应用程序正常工作。 - Azuken
我已经按照教程完成了它:http://stackoverflow.com/questions/32208154/custom-ratingbar-showing-five-stars-despite-setrating - jean d'arme
2个回答

2
我遇到了同样的问题。我下载了代码,自定义评分栏文件中的项目如下所示:
<item
    android:id="@+android:id/background"
    android:drawable="@drawable/star_blank"/>

为了消除错误,我将ID更改为

 <item
 android:id="@+id/background"
 android:drawable="@drawable/star_blank"/>

这段代码删除了错误,但只显示了所选项目的星级,而评分条的值仍在变化。

因此,我将自定义评分栏中的id更改为

<item
android:id="@android:id/background"
android:drawable="@drawable/star_blank"/>

这个很完美地工作了。


0

尝试这个,将进度绘制作为评分条的属性设置:

                        <RatingBar

                        android:numStars="5"
                        android:progressDrawable="@drawable/ratingbar_selector"
                        android:stepSize="0.2" />

最终我找到了解决方案:我将标签“@+android:id”替换为“@+id”,这就是为什么它没有起作用的原因。无论如何,还是谢谢! - Azuken

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