ratingbar android setMax()不起作用

时间:2013-11-18 13:05:29

标签: android ratingbar

我有自定义'RatingBar'。

<RatingBar
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    style="@style/foodRatingBar"
                    android:numStars="5"
                    android:stepSize="1.0"
                    android:id="@+id/article_rating_value_rB"
                    android:layout_alignRight="@+id/article_fit_value_tV"
                    android:isIndicator="false"
                    android:layout_alignBottom="@+id/article_rating_tV"/>

'foodRatingBar':

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <!--Rating bar ArticleFragment style-->
    <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/ratingstars</item>
        <item name="android:minHeight">24dp</item>
        <item name="android:maxHeight">24dp</item>
    </style>
</resources>

'ratingstars':

<?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/ic_star" />
    <item android:id="@android:id/secondaryProgress"
          android:drawable="@drawable/ic_star" />
    <item android:id="@android:id/progress"
          android:drawable="@drawable/ic_star_p" />
</layer-list>

但我遇到了下一个问题:

    rating = (RatingBar) getActivity().findViewById(R.id.article_rating_value_rB);
    rating.setMax(4);
    rating.setNumStars(4);
    rating.setRating(2);
    Log.d(LOG_TAG, "r = " + rating.getRating() + 
    "  ns = " + rating.getNumStars() + 
    "  m = " + rating.getMax());

结果是:

enter image description here

r = 2.4

ns = 4

m = 5

1 个答案:

答案 0 :(得分:0)

我希望setNumStars是你想要的。

<强>更新

我的代码:

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="4" />

</LinearLayout>

的活动:

mRatingBar = (RatingBar) findViewById(R.id.ratingBar1);
mRatingBar.setNumStars(6);

适合我。

enter image description here

我希望这会对你有所帮助。