我有一个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星。
最佳答案
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 numStars时显示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"/>
关于android - ratingbar无法显示当前numStars,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36763935/