How to make RatingBar to show five stars

前端 未结 18 476
栀梦
栀梦 2020-12-04 18:58

I am following the standard example of how to add a RatingBar. To control the number of stars I tried to use android:numStars=\"5\". The problem is

相关标签:
18条回答
  • 2020-12-04 19:06

    If you are using

    android:layout_width="match_parent"

    Use wrap_content and it will only show the set numStars :)

    android:layout_width="wrap_content"

    0 讨论(0)
  • 2020-12-04 19:07

    The default value is set with andoid:rating in the xml layout.

    0 讨论(0)
  • 2020-12-04 19:08

    The RatingBar.setNumStar documentation says:

    Sets the number of stars to show. In order for these to be shown properly, it is recommended the layout width of this widget be wrap content.

    So setting the layout width to wrap_content should solve this problem.

    0 讨论(0)
  • 2020-12-04 19:08
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <RatingBar
            android:id="@+id/ruleRatingBar"
            android:isIndicator="true"
            android:numStars="5"
            android:stepSize="0.5"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-04 19:13

    This worked for me: RatingBar should be inside LinearLayout other than having layout width set to wrap content for RatingBar.

    0 讨论(0)
  • 2020-12-04 19:17

    Another possibility is you are using the Rating in Adapter of list view

    View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
    

    Below params are necessary for all the attributes to work properly:

    1. Set the root to parent
    2. boolean attachToRoot as false

    Ex: convertView = inflater.inflate(R.layout.myId, parent, false);

    0 讨论(0)
提交回复
热议问题