How to make RatingBar to show five stars

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

提交回复
热议问题