How to make RatingBar to show five stars

前端 未结 18 478
栀梦
栀梦 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:18

    I found that the RatingBar stretched to a maximum number of stars because it was enclosed in a Table with the attribute android:stretchColumns = "*".

    Once I took stretchCoulumns off all columns, the RatingBar displayed according to the android:numStars value

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

    You should just use numStars="5" in your XML, and set android:layout_width="wrap_content".
    Then, you can play around with styles and other stuff, but the "wrap_content" in layout_width is what does the trick.

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

    Even i was facing the issue @Roland , I had included one more attribute called

    android:layout_alignParentRight="true" 
    

    in my RatingBar declaration in XML. This attribute prevented from setting of the stars required and setting up the NumStars

    Keep posted on the issues you come across !

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

    To show a simple star rating in round figure just use this code

    public static String getIntToStar(int starCount) {
            String fillStar = "\u2605";
            String blankStar = "\u2606";
            String star = "";
    
            for (int i = 0; i < starCount; i++) {
                star = star.concat(" " + fillStar);
            }
            for (int j = (5 - starCount); j > 0; j--) {
                star = star.concat(" " + blankStar);
            }
            return star;
        }
    

    And use it like this

    button.setText(getIntToStar(4));

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

    in code

    mRatingBar.setRating(int)
    
    0 讨论(0)
  • 2020-12-04 19:26
    <RatingBar
                        android:id="@+id/ratingBar"
                        style="@style/custom_rating_bar"
                        android:layout_width="wrap_content"
                        android:layout_height="35dp"
                        android:clickable="true"
                        android:numStars="5" 
                        />
    

    Configure the number of Stars in the XML file... No need to provide it in Styles or Activity/Fragment .... IMPORTANT: Make sure you Put the WIDTH as Wrap content and weights are not enabled

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