Android RatingBar 更改星星颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2446270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:53:12  来源:igfitidea点击:

Android RatingBar change star colors

androidrating

提问by d-man

How can i change the star colors and how can i change the size of the stars ?

如何更改星星的颜色以及如何更改星星的大小?

采纳答案by CommonsWare

Step #1: Create your own style, by cloning one of the existing styles (from $ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml), putting it in your own project's styles.xml, and referencing it when you add the widget to a layout.

第 1 步:创建您自己的样式,方法是克隆现有样式之一(来自$ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml),将其放入您自己项目的 中styles.xml,并在将小部件添加到布局时引用它。

Step #2: Create your own LayerDrawableXML resources for the RatingBar, pointing to appropriate images to use for the bar. The original styles will point you to the existing resources that you can compare with. Then, adjust your style to use your own LayerDrawableresources, rather than built-in ones.

第 2 步:为 . 创建您自己的LayerDrawableXML 资源RatingBar,指向要用于该栏的适当图像。原始样式将指向您可以比较的现有资源。然后,调整您的风格以使用您自己的LayerDrawable资源,而不是内置资源。

回答by Alex

It's a little complicated at the mentioned blog, I've used a similar but simplier way. You do need 3 star images (red_star_full.png, red_star_half.png and red_star_empty.png) and one xml, that's all.

上面提到的博客有点复杂,我使用了类似但更简单的方法。您确实需要 3 张明星图像(red_star_full.png、red_star_half.png 和 red_star_empty.png)和一个 xml,仅此而已。

Put these 3 images at res/drawable.

将这 3 个图像放在 res/drawable 中。

Put there the following ratingbar_red.xml:

将以下 ratingbar_red.xml 放在那里:

<?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, finally, tell your ratingbar definition to use this, i.e.

最后,告诉您的 ratingbar 定义使用它,即

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

That's it.

就是这样。

回答by Mirek Michalak

Try this, if you only want to change color:

试试这个,如果你只想改变颜色:

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

回答by Vishal Kumar

The easiest way that worked for me...if you are extending AppCompat Activity

对我有用的最简单的方法......如果你正在扩展 AppCompat Activity

In your build.gradle add latest appcompat library.

在你的 build.gradle 添加最新的 appcompat 库。

dependencies {  
    compile 'com.android.support:appcompat-v7:X.X.X' // where X.X.X version
}

Make your activity extend android.support.v7.app.AppCompatActivity

使您的活动扩展 android.support.v7.app.AppCompatActivity

public class MainActivity extends AppCompatActivity {  
    ...
}

Declare custom style in your styles.xml file.

在styles.xml 文件中声明自定义样式。

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

Apply this style to your RatingBar via android:theme attribute.

通过 android:theme 属性将此样式应用于您的 RatingBar。

<RatingBar  
    android:theme="@style/RatingBar"
    android:rating="3"
    android:stepSize="0.5"
    android:numStars="5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

回答by lgvalle

2015 Update

2015年更新

Now you can use DrawableCompatto tint all kind of drawables. For example:

现在您可以使用DrawableCompat为所有类型的可绘制对象着色。例如:

Drawable progress = ratingBar.getProgressDrawable();
DrawableCompat.setTint(progress, Color.WHITE);

This is backwards compatible up to API 4

这向后兼容 API 4

回答by superpuccio

From the API 21on it's very easy to change the color of the stars with this three lines of code:

从 API 21开始,使用以下三行代码可以很容易地更改星星的颜色:

android:progressTint="@android:color/holo_red_dark"
android:progressBackgroundTint="@android:color/holo_red_dark"
android:secondaryProgressTint="@android:color/holo_red_dark" 

Doing it like this, you'll change:

这样做,你会改变:

  • the filled stars color (progressTint)
  • the unfilled stars color (progressBackgroundTint)
  • and the border color (secondaryProgressTint) of the stars
  • 填充的星星颜色(progressTint)
  • 未填充的星星颜色 (progressBackgroundTint)
  • 和星星的边框颜色 (secondaryProgressTint)

回答by Yuriy Vinogradov

If you want to change color for all stars states you my use:

如果您想更改所有星星的颜色,请说明我的用途:

LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(getResources().getColor(R.color.starFullySelected), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(getResources().getColor(R.color.starPartiallySelected), PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(getResources().getColor(R.color.starNotSelected), PorterDuff.Mode.SRC_ATOP);

回答by dirkoneill

The solutions that Alex and CommonsWares have posted are correct. One thing that the Android never talks about though is proper pixel sizes for different densities. Here are the required dimensions for each density based on halo light.

Alex 和 CommonsWares 发布的解决方案是正确的。Android 从不谈论的一件事是不同密度的适当像素大小。以下是基于光晕光的每种密度所需的尺寸。

Small Star

小星星

mdpi: 16px
hdpi: 24px
xhdpi: 32px
xxhdpi: 48px

Medium Star

中星

mdpi: 24px
hdpi: 36px
xhdpi: 48px
xxhdpi: 72px

Large Star

大明星

mdpi: 35px
hdpi: 52px
xhdpi: 69px
xxhdpi: 105px

回答by box

So I have been struggling with this issue for two hours and I have come up with a working solution for all API versions, where half stars ratings are also shown.

所以我一直在为这个问题苦苦挣扎两个小时,我为所有 API 版本提出了一个可行的解决方案,其中还显示了半星评级。

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

You call the method with this order of drawables:

您可以按照以下顺序调用该方法:

    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    // Filled stars
    setRatingStarColor(stars.getDrawable(2), ContextCompat.getColor(getContext(), R.color.foreground));
    // Half filled stars
    setRatingStarColor(stars.getDrawable(1), ContextCompat.getColor(getContext(), R.color.background));
    // Empty stars
    setRatingStarColor(stars.getDrawable(0), ContextCompat.getColor(getContext(), R.color.background));

NOTE: Also you must specify attributes "max" and "numStars" in XML, otherwise half stars aren't shown.

注意:您还必须在 XML 中指定属性“max”和“numStars”,否则不会显示半颗星。

回答by Dan Dar3

Now you can use DrawableCompatfrom AppCompat v22.1.0 onwards to dynamically tint all kind of drawables, useful when you're supporting multiple themes with a single set of drawables. For example:

现在,您可以从 AppCompat v22.1.0 开始使用DrawableCompat来动态着色所有类型的可绘制对象,这在您使用一组可绘制对象支持多个主题时非常有用。例如:

LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable();
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(0)), Color.RED);   // Empty star
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(1)), Color.GREEN); // Partial star
DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable.getDrawable(2)), Color.BLUE);  // Full star

This is backwards compatible down to API 4. Also see Chris Banes' blog post on Support Libraries v22.1.0

这向下兼容 API 4。另请参阅 Chris Banes 在支持库 v22.1.0上的博客文章

For the actual size and shape you will need to define a new style and layer-list drawables for the appropriate size, as others have already answered above.

对于实际大小和形状,您需要为适当大小定义新样式和图层列表可绘制对象,正如其他人已经在上面回答的那样。