Android RatingBar(填满下一颗星) [英] Android RatingBar (filling next star)
问题描述
我有以下RatingBar:
I have the following RatingBar:
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:numStars="5"
android:max="5"
android:stepSize="1.0"
android:layout_marginTop="12dp"/>
当我按下星星的左侧部分时-被选中,但是当我按下右侧的部分时-下一个星星被选中.我需要单击的星标才能被选中.请帮助我了解问题所在.
When I press the left part of a star - it is selected, but when I press the right part - the following star is selected. I need the star I click on to be selected whichever part of it I click. Help me please to understand what is wrong.
推荐答案
RatingBar
扩展 ProgressBar
可能是不幸的副作用,该方法内部使用了int
进位(而不是 ceil
)的舍入过程从 float
定位位置移至 int
进度.我发现最好的解决方案是将 stepSize
设置为 0.01
,而不是 1
,然后设置
It is perhaps an unfortunate side effect of RatingBar
extending ProgressBar
, which internally uses a int
progress with rounding (rather than ceil
) to go from a float
seek position to a int
progress. The best solution I have found is to set your stepSize
to 0.01
rather then 1
, then set
rb.setOnRatingBarChangeListener( new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged( final RatingBar ratingBar, final float rating, final boolean fromUser ) {
if ( fromUser ) {
ratingBar.setRating( Math.ceil(rating) );
}
}
});
这篇关于Android RatingBar(填满下一颗星)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!