0

我在我的应用程序中制作了一个评分栏,它使用自定义样式,使用我自己的星星。

我的评分栏定义如下:

 <RatingBar
           android:layout_width="wrap_content"
           android:layout_height="15dp"
           android:id="@+id/ratingBar"
           android:numStars="10"
           android:stepSize="1"
           android:rating="10"
           android:layout_marginTop="5dp"
           android:layout_below="@id/feedbackTable"
           android:layout_toRightOf="@id/ratingText"
           android:layout_marginLeft="5dp"
           style="@style/starBar" />

我在 styles.xml 中使用的样式是:

<style name="starBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingstars</item>
        <item name="android:minHeight">22dip</item>
        <item name="android:maxHeight">22dip</item>
    </style>

使用 ratingstars.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/ratingstars_full_empty" />
    <item android:id="@+id/secondaryProgress" android:drawable="@drawable/ratingstars_full_empty" />
    <item android:id="@+id/progress" android:drawable="@drawable/ratingstars_full_filled" />
</layer-list>

当我调整 android:rating 时,它会在预览中显示正确数量的星星填充和空白。但是,使用我的模拟器或真实设备时,该栏完全充满了星星 (10/10)。我尝试使用以下代码以编程方式对其进行调整:

 ratingbar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                Log.d("rating", rating+"");
                ratingbar.setRating(rating);
            }
        });

现在来自 logcat 的日志实际上显示评级发生了变化,但条形图并未在视觉上更新自身,例如 6 颗星已满,4 颗星为空,我不知道为什么。

可绘制对象是正确的,因为预览也显示它是正确的,那么是什么原因导致的呢?

编辑:

这是 ratingstars_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" />

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

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

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

</selector>

这里是 ratingstars_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_empty" />

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

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

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

</selector>
4

2 回答 2

3

您应该将 ratingstars.xml 中的 id 属性从 更改"@+id/...""@android:id/...",因为资源是 android 系统的一部分,并且已经在默认的 android progress drawable 中定义。

<?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/ratingstars_full_empty" />
    <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/ratingstars_full_empty" />
    <item android:id="@android:id/progress" android:drawable="@drawable/ratingstars_full_filled" />
</layer-list>
于 2015-01-12T21:31:08.670 回答
1

看起来这不是调用侦听器的问题,而只是android:progressDrawable您应用到它的自定义样式属性。我的猜测是您正在使用的可绘制对象正在做意想不到的事情。尝试style="@style/starBar"从 RatingBar 中删除,看看是否按预期工作。如果是这样,那是你的问题

于 2013-12-15T19:20:26.277 回答