Android:将ratingbar的颜色更改为golden

时间:2015-09-27 16:46:32

标签: android ratingbar

我想将评级栏的颜色改为黄金 我不想自定义星星,我只想更改API 16或更高版本的颜色
我试过以下解决方案,但没有一个能为我解决

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

14 个答案:

答案 0 :(得分:88)

此选项现在包含在AppCompat库中。 RatingBar的AppCompat版本会自动使用。

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

示例(来自我自己的应用程序):

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

主题:

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

答案 1 :(得分:12)

在API 21及更高版本中,您可以使用

更改填充星星的颜色
     android:progressTint="@color/color"

的笔画颜色
     android:progressBackgroundTint="@color/color"

答案 2 :(得分:8)

正如您所提到的,您想要在不使用drawable的情况下更改星形颜色。您可以通过以下几行代码来完成。

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

答案 3 :(得分:5)

我的任务是将评分栏的颜色更改为Android &gt; = 14 SDK 的应用主要颜色。我在代码和xml文件中解决了这个问题。

 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();
        }
    }

在xml文件中

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

答案 4 :(得分:3)

试试这个,

您需要3个星图(red_star_full.png,red_star_half.png和red_star_empty.png)和一个xml文件。如果你想要金星,那么使用金色的星形图像。这是红星。

你可以将ratingbar_color.xml放在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>

并在评级栏定义中使用以下内容。

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

答案 5 :(得分:2)

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);

答案 6 :(得分:2)

我的用例是针对不同的评分显示不同的颜色。例如:

1-红色
2-橙色
3-黄色 4-浅绿色
5-深绿色

解决方案:

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

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

然后在您的setCurrentRating()方法中:

 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));

    }
  }

然后在您的setRatingStarColor()方法中:

   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);
    }
  }

感谢answer这帮我解决了这个问题。 希望它可以帮助别人!

答案 7 :(得分:1)

简单的解决方案,使用 AppCompatRatingBar 并使用以下代码

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

答案 8 :(得分:1)

我找到的最简单的解决方案是更改color.xml中颜色“ colorAccent”的颜色定义。无需进一步编码,这就是全部魔术。

答案 9 :(得分:1)

如果您在kitkat上运行,则默认情况下显示的是蓝色星星,而不是黄色和金色,请添加以下行:style="@style/Base.Widget.AppCompat.RatingBar.Small"

答案 10 :(得分:0)

使用此代码可以跳过

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

答案 11 :(得分:0)

来自XML

android:progressTint="#ffff8800"

以编程方式:

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

答案 12 :(得分:0)

等级栏颜色更改和大小更改:

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

答案 13 :(得分:0)

使用Kotlin Extensions的其他解决方案,可以根据等级范围设置不同的颜色:

扩展功能和属性:

var RatingBar.colorScheme: Map<Float, Int>?
    get() = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>?
    set(value) = setTag(R.id.rating_bar_color_scheme, value)

fun RatingBar.setColorByRating(rating: Float) {
    val colorScheme = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>
    colorScheme[rating]?.also { color -> setLayerColor(2, color) }
    setLayerColor(1, ContextCompat.getColor(context, android.R.color.transparent))
    setLayerColor(0, ContextCompat.getColor(context, android.R.color.darker_gray))
}

fun RatingBar.setLayerColor(layerIndex: Int, color: Int) {
    val drawable = progressDrawable as LayerDrawable
    drawable.getDrawable(layerIndex).also {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            DrawableCompat.setTint(it, color)
        } else {
            it.setColorFilter(color, PorterDuff.Mode.SRC_IN)
        }
    }
}

要使用它:

val red = ContextCompat.getColor(requireContext(), R.color.redColor)
val yellow = ContextCompat.getColor(requireContext(), R.color.yellowColor)
val green = ContextCompat.getColor(requireContext(), R.color.greenLightColor)

rbStars?.colorScheme = mapOf(
        1.0F to red,
        2.0F to red,
        3.0F to yellow,
        4.0F to green,
        5.0F to green
)

rbStars?.setColorByRating(rating)