如何设置RatingBar的星星颜色?

21

如何设置RatingBar中的星星颜色?我想要黄色的星星。


试试这个:Android RatingBar 更改星星颜色 - Vikram
如果您正在使用AppCompat活动,请查找最简单的方法 - Vishal Kumar
10个回答

22

最简单的方法:

android:progressTint="@color/color"

光滑而有光泽。


1
@suku 是的,但问题没有提及有关minSDK的限制。 - MiguelCatalan

12

我发现仅设置progressTint不是一个完整的解决方案。如果您的stepSize小于1,它将改变星号的颜色,但不会对部分填充的星号进行更改。根据我的经验,您还需要设置secondaryProgressTint以防止默认的星号tint再次出现。这是我的代码:

android:progressTint="@color/accent"
android:secondaryProgressTint="@android:color/transparent"

希望这能对处于我这种情况的人有所帮助。


1
你还需要设置android:progressBackgroundTint来为空星形设置背景色。 - chubao
3
еұһжҖ§progressTintе’ҢsecondaryProgressTintд»…йҖӮз”ЁдәҺAPIзә§еҲ«21еҸҠжӣҙй«ҳзүҲжң¬гҖӮ - ViliusK
@ViliusK,使其适用于所有Android API版本的最佳方法是什么? - Pallavi

12

这对我起作用:

    Drawable drawable = ratingBar.getProgressDrawable();
    drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);

我喜欢这个答案,因为你不需要自定义星星。我只需要改变颜色,而不是星星。唯一的缺点是只有一种颜色,我想其他可绘制对象也需要修改。 - Jayshil Dave

6

你是否尝试过使用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/star_empty_show"/>

    <item
    android:id="@android:id/secondaryProgress"
    android:drawable="@drawable/star_empty_show"/>

    <item
    android:id="@android:id/progress"
    android:drawable="@drawable/star_filled_show"/>

</layer-list>

在您的xml中调用它

<RatingBar
        android:id="@id/rate"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5.0dip"
        android:isIndicator="true"
        android:max="5"
        android:numStars="5"
        android:progressDrawable="@drawable/ratebar_theme"
        android:stepSize="0.1" />

你想使用自己的可绘制资源吗?


嗨,kwishnu,我尝试了这个解决方案,但是当我尝试将评分设置为1.1、1.2等时,它显示为1.5。我已经准备好了三张星级图片,一张为空的,一张半满的,一张满的。我想它应该在它们之间插值以获得小数部分,我只是想知道是否有其他人遇到了同样的问题,或者我做错了什么明显的事情? - Graham Baitson

4

您需要三张星形图片(star_filled_show.png、star_half_show.png 和 star_empty_show.png)和一个 xml 文件,就这些。

将这三张图片放入 res/drawable 目录下。

然后将以下的 ratingbarstars.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/star_empty_show"/>

<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_half_show"/>

<item
android:id="@android:id/progress"
android:drawable="@drawable/star_filled_show"/>
</layer-list>

告诉你的评分条定义使用这个可绘制物。
<RatingBar android:progressDrawable="@drawable/ratingbarstars.xml"/>

2

评分条在触摸星星时会自动变色。

首先,在app\src\main\res\values\styles.xml文件中添加样式:

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

然后您可以像这样为评分栏添加主题:
<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"/>

1

在提到的博客中可能有些复杂,我使用了一种类似但更简单的方法。你需要3个星级图片(red_star_full.png、red_star_half.png和red_star_empty.png)和一个xml文件,就这些。

将这3个图片放在res/drawable文件夹中。

将以下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>

最后,告诉您的评分栏定义使用它,即:

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

就是这样。


1
当我这样做时,我也必须加上TintMode。
<RatingBar
        android:progressTint="@color/colorAccent"
        android:progressTintMode="src_atop" ---- This is important!
        android:secondaryProgressTint="@color/colorPrimary"
        android:secondaryProgressTintMode="src_atop" ---- This is important!
        android:progressBackgroundTint="@color/black_alpha"/>

仅适用于API 21及以上版本


1

试试这个

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

当星星被填充了一半时怎么办?我希望星星是一半黄色,一半灰色 - 或者其他什么颜色。 - iOSAndroidWindowsMobileAppsDev
我对此并没有太多的想法,但是你可以使用梯度或者PorterDuff模式来实现。 - Anand Savjani

0
<RatingBar
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/RatingBar"/>

使用样式主题

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接