Android: Change color of ratingbar to golden

假如想象 提交于 2019-11-27 05:21:28

问题


I want to change color of rating bar to golden.
I dont want to customize the stars, i just want to change the color for API 16 or above
I have tried following solutions but none of them worked out for me

1.    

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

2.

android:progressDrawable="@color/golden" in XML

回答1:


This option is now included in the AppCompat library. The AppCompat version of the RatingBar is used automatically.

http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html

Example (from my own app):

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

With theme:

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



回答2:


On API 21 and higher, you can change the color of the filled stars with

     android:progressTint="@color/color"

and the color of the stroke with

     android:progressBackgroundTint="@color/color"



回答3:


As you mentioned that you want to change the star color without using the drawable. You can do it by few lines of code as below.

RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#0064A8"),PorterDuff.Mode.SRC_ATOP);



回答4:


My task was change color of Rating Bar to the app's primary color for Android >=14 SDK. I've solved this problem in code and in xml file.

 mRatingBar = (RatingBar)view.findViewById(R.id.ratingBar);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        try {
            Drawable progressDrawable = mRatingBar.getProgressDrawable();
            if (progressDrawable != null) {
                DrawableCompat.setTint(progressDrawable, ContextCompat.getColor(getContext(), R.color.colorPrimary));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And in xml file

        <RatingBar
            android:id="@+id/ratingBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:backgroundTint="@color/colorPrimary"
            android:numStars="5"
            android:progressTint="@color/colorPrimary"
            android:rating="0"
            android:secondaryProgressTint="@android:color/transparent"
            android:stepSize="1" />



回答5:


try this,

You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml file. If you want to golden star then use golden star image. this is for red star.

you can put ratingbar_color.xml in res/drawable.

<?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 in rating bar definition use following.

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



回答6:


My use case was to show different colors for different ratings. For example :

1- Red
2- Orange
3- Yellow
4- Light Green
5- Dark Green


Solution :

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

  @Override
  public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
      setCurrentRating(rating);
    }
});

Then in your setCurrentRating() method :

 private void setCurrentRating(float rating) {
    LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
    if(mContext!=null) {
          switch (Math.round(rating)) {
            case 1:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
              break;
            case 2:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
              break;
            case 3:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
              break;
            case 4:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
              break;
            case 5:
              setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
              break;
          }
      setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
      setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));

    }
  }

Then in your setRatingStarColor() method:

   private void setRatingStarColor(Drawable drawable, @ColorInt int color)
  {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
      DrawableCompat.setTint(drawable, color);
    }
    else
    {
      drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }
  }

Thanks to this answer which helped me solve this. Hope it helps someone !




回答7:


RatingBar ratingreview; 
ratingreview=(RatingBar)v.findViewById(R.id.ratingreview); 
ratingreview.setRating(Float.parseFloat(objrating)); 
Drawable drawable = ratingreview.getProgressDrawable(); 
drawable.setColorFilter(Color.parseColor("#6A9A28"), PorterDuff.Mode.SRC_ATOP);



回答8:


Simple solution, use AppCompatRatingBar and use below code

ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));



回答9:


The simplest solution I have found is changing the color definition for the color "colorAccent" in the color.xml. That's entire magic without further coding.




回答10:


From XML

android:progressTint="#ffff8800"

Programmatically:

Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"), 
PorterDuff.Mode.SRC_ATOP);



回答11:


Use this code to get over the line

Drawable drawableReview = writeReviewRatingBar.getProgressDrawable();
        drawableReview.setColorFilter(Color.parseColor("#FFFDD433"), PorterDuff.Mode.SRC_ATOP);



回答12:


Rating bar color change and size change:

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    style = "?android:attr/ratingBarStyleIndicator"
    android:progressTint="#ffff8800"
    android:rating="3.5"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:theme="@style/RatingBar" />


来源:https://stackoverflow.com/questions/32810341/android-change-color-of-ratingbar-to-golden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!