How can I set the star color of the ratingbar?

后端 未结 10 1661
夕颜
夕颜 2021-02-12 19:54

How can I set the star color of the ratingbar? I want yellow stars.

相关标签:
10条回答
  • 2021-02-12 20:32
    <RatingBar
            android:id="@+id/rating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/RatingBar"/>
    

    with theme in styles

    <style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorControlActivated">@color/yellow</item>
    

    0 讨论(0)
  • 2021-02-12 20:33

    You do need 3 star images (star_filled_show.png, star_half_show.png and star_empty_show.png) and one xml, that's all.

    Put these 3 images into res/drawable.

    Put there the following ratingbarstars.xml:

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

    Tell your ratingbar definition to use this drawable

    <RatingBar android:progressDrawable="@drawable/ratingbarstars.xml"/>
    
    0 讨论(0)
  • 2021-02-12 20:34

    try this one

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    stars.getDrawable(2).setColorFilter(Color.YELLOW,PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2021-02-12 20:38

    It's a little complicated at the mentioned blog, I've used a similar but simplier way. You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.

    Put these 3 images at res/drawable.

    Put there the following ratingbar_red.xml:

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

    and, finally, tell your ratingbar definition to use this, i.e.

    <RatingBar android:progressDrawable="@drawable/ratingbar_red"/>
    

    That's it.

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