ratingbar无法显示当前的numStars

时间:2016-04-21 08:28:22

标签: android ratingbar

我有一个RatingBar并使用方法setNumStars(final int numStars)。  在XML中,代码是这样的:

 <RatingBar
                android:id="@+id/rating_bar"
                style="@style/CMYMyRatingBars"
                android:isIndicator="true"
                android:numStars="5"
                android:rating="5"
                />

然后出现问题。当numStars为0.我的RatingBar将显示5颗星。

1 个答案:

答案 0 :(得分:1)

private RatingBar ratingBar;
private float ratingValue;

ratingBar = (RatingBar) findViewById(R.id.rating_bar);

ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 

                ratingValue = rating;

            }
        });

ratingBar.getRating();



<RatingBar
    android:id="@+id/rating_bar"
    style="@style/CMYMyRatingBars"
    android:isIndicator="true"
    android:numStars="0"
    android:rating="4"
     />

因为您给出了等级5,这就是为什么当您给出0个数字时它显示5等级的原因。我已执行此代码并且代码工作正常。您必须看到零评级然后实现此代码。

<RatingBar
    android:id="@+id/rating_bar"
    style="@style/CMYMyRatingBars"
    android:isIndicator="true"
    android:numStars="0"
    android:rating="0"
     />

样式

<style name="CMYMyRatingBars" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingbar</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">35dp</item>
        <item name="android:gravity">center</item>
    </style>

ratingbar.xml

<item
    android:id="@+android:id/background"
    android:drawable="@drawable/ratingbar_empty"/>
<item
    android:id="@+android:id/secondaryProgress"
    android:drawable="@drawable/ratingbar_empty"/>
<item
    android:id="@+android:id/progress"
    android:drawable="@drawable/ratingbar_filled"/>

ratingbar_filled.xml

<item android:drawable="@drawable/ratingbar_staron" android:state_pressed="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staron" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staron" android:state_selected="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staron"/>

ratingbar_empty.xml

<item android:drawable="@drawable/ratingbar_staroff" android:state_pressed="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staroff" android:state_focused="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staroff" android:state_selected="true" android:state_window_focused="true"/>
<item android:drawable="@drawable/ratingbar_staroff"/>