How can I set the star color of the ratingbar?

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

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

相关标签:
10条回答
  • 2021-02-12 20:15

    Have you tried an xml theme, such as

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

    calling it in your xml as

    <RatingBar
            android:id="@id/rate"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5.0dip"
            android:isIndicator="true"
            android:max="5"
            android:numStars="5"
            android:progressDrawable="@drawable/ratebar_theme"
            android:stepSize="0.1" />
    

    and using your own drawables?

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

    The simpliest way:

    android:progressTint="@color/color"
    

    Smooth and shiny.

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

    I've found that just setting the progressTint is not a complete solution. It will change the color of the star but not on partial star fills if your stepSize is less than 1. In my experience you also need to set the secondaryProgressTint in order to stop the default star tint from rearing its ugly head. Here is my code:

    android:progressTint="@color/accent"
    android:secondaryProgressTint="@android:color/transparent"
    

    Hope this helps someone in my situation.

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

    The rating bar is used automatically at run time for change color on touch star.

    First add style in app\src\main\res\values\styles.xml file:

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

    Then your rating bar add theme like this:

    <RatingBar
    android:id="@+id/rating"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1"
    android:theme="@style/RatingBar"/>
    
    0 讨论(0)
  • 2021-02-12 20:27

    This worked for me:

        Drawable drawable = ratingBar.getProgressDrawable();
        drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);
    
    0 讨论(0)
  • 2021-02-12 20:28

    When I did it, and I had to put the TintMode too.

    <RatingBar
            android:progressTint="@color/colorAccent"
            android:progressTintMode="src_atop" ---- This is important!
            android:secondaryProgressTint="@color/colorPrimary"
            android:secondaryProgressTintMode="src_atop" ---- This is important!
            android:progressBackgroundTint="@color/black_alpha"/>
    

    only for api version 21 and above

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