2016-01-04 1 views
0

J'essaie de changer la couleur des étoiles en jaune.
Je ne veux pas utiliser un autre style (!), Juste changer la couleur des étoiles intégrées dans la RatingBar.Changer (seulement) la couleur de RatingBar

J'ai lu quelques tutoriels comment changer le style, ils sont tous assez similaires. Mais pour une raison quelconque, ça ne marche pas pour moi.
Quoi qu'il en soit, puisque je veux juste changer la couleur, et non tout le style, je le code ci-dessous:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_details); 
     rbBookRating = (RatingBar) findViewById(R.id.rbRating); 
     rbBookRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { 
      @Override 
      public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 

      } 
     }); 
     LayerDrawable stars = (LayerDrawable) rbBookRating.getProgressDrawable(); 
     stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP); 
     stars.getDrawable(1).setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP); 
     stars.getDrawable(0).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); 
    } 

mise en page

<RatingBar 
android:id="@+id/rbRating" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:isIndicator="true" 
android:stepSize="0.5" /> 

(Il semble que) Mon application se bloque juste après this ligne:

LayerDrawable stars = (LayerDrawable) rbBookRating.getProgressDrawable(); 

Une suggestion?

+2

Pouvez-vous ajouter l'erreur logcat que vous voyez? – Shmuel

+2

veuillez ajouter logcat @Nick –

Répondre

1

Je avais juste besoin d'utiliser:

DrawableCompat.setTint(rbBookRating.getProgressDrawable(), Color.YELLOW); 

Merci quand même ...

0

créer d'abord un thème

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

Ensuite, ajoutez dans votre xml

<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"/>