Android:将ratingbar的颜色更改为金色

Android:将ratingbar的颜色更改为金色,android,ratingbar,Android,Ratingbar,我想将分级栏的颜色更改为金色。 我不想定制星星,我只想更改API 16或更高版本的颜色 我尝试过以下解决方案,但没有一个对我有效 1. RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar); LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable(); stars.getDrawable(2).setColorFilter(Color.

我想将分级栏的颜色更改为金色。
我不想定制星星,我只想更改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
试试这个

您确实需要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/>

正如您所提到的,您希望在不使用绘图工具的情况下更改星形颜色。您可以通过以下几行代码来完成

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

此选项现在包含在AppCompat库中。 自动使用分级栏的AppCompat版本

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


主题:

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

@颜色/暗黄色
@颜色/浅灰色

我的任务是将评级栏的颜色更改为Android应用程序的主颜色=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();
        }
    }
mRatingBar=(RatingBar)视图.findViewById(R.id.RatingBar);
if(Build.VERSION.SDK\u INT
和在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" />

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

     android:progressTint="@color/color"
以及笔画的颜色

     android:progressBackgroundTint="@color/color"

我的用例是为不同的评级显示不同的颜色。例如:

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

    }
  }
然后在您的
设置StarColor()
方法中:

   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);
    }
  }
多亏了这个帮助我解决了这个问题。
希望它能帮助别人

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

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

我发现的最简单的解决方案是更改color.xml中颜色“colorAccent”的颜色定义。这就是没有进一步编码的全部魔力

使用此代码越过这条线

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

android:progressTint="#ffff8800"
以编程方式:

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

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

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


如果您在kitkat上运行,默认情况下显示蓝色星星而不是黄色和金色,则添加以下行:
style=“@style/Base.Widget.AppCompat.RatingBar.Small”
使用Kotlin扩展的其他解决方案,允许根据评级范围设置不同的颜色:

扩展函数和属性:

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

例如,它的副本绘制了5个彩色星星,而不是3.5。在上面的答案中,使用style=“@style/Base.Widget.AppCompat.RatingBar.Small”或评级栏小部件中的@style/Base.Widget.AppCompat.RatingBar.Indicator来处理评级星星的颜色和大小。有什么方法可以通过编程实现这一点吗?我需要在runtimenevermind上更改星号颜色。在这里发现了一个很棒的自由:我们应该给这个人一枚奖章。可定制性已经过时了rooftop@Malder您必须将
android:
colorControlActivated
中删除。因为我使用了自动完成代码,所以添加了
android:
前缀。如果没有该前缀,它也适用于KitKat。评级栏有3种颜色,您错过的是当选择0星时。我不知道答案的关键(y)
<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" />
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)