Java 如何设置评级栏的星号颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18008121/
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
How can I set the star color of the ratingbar?
提问by Finn Su
How can I set the star color of the ratingbar? I want yellow stars.
如何设置评级栏的星号颜色?我想要黄色的星星。
回答by kwishnu
Have you tried an xml theme, such as
您是否尝试过 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>
calling it in your xml as
在你的 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" />
and using your own drawables?
并使用您自己的可绘制对象?
回答by OrchTech
You do need 3 star images (star_filled_show.png, star_half_show.png and star_empty_show.png) and one xml, that's all.
您确实需要 3 张明星图像(star_filled_show.png、star_half_show.png 和 star_empty_show.png)和一个 xml,仅此而已。
Put these 3 images into res/drawable
.
将这 3 张图像放入res/drawable
.
Put there the following ratingbarstars.xml
:
把以下内容放在那里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>
Tell your ratingbar definition to use this drawable
告诉您的评级栏定义使用此 drawable
<RatingBar android:progressDrawable="@drawable/ratingbarstars.xml"/>
回答by Suneel Kumar
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 Anand Savjani
try this one
试试这个
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW,PorterDuff.Mode.SRC_ATOP);
回答by Chad Bingham
This worked for me:
这对我有用:
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);
回答by MiguelCatalan
The simpliest way:
最简单的方法:
android:progressTint="@color/color"
Smooth and shiny.
光滑有光泽。
回答by Franklin Kesler
I've found that just setting the progressTint is not a complete solution. It will change the color of the star but not on partial star fills if your stepSize is less than 1. In my experience you also need to set the secondaryProgressTint in order to stop the default star tint from rearing its ugly head. Here is my code:
我发现仅仅设置 progressTint 并不是一个完整的解决方案。如果您的 stepSize 小于 1,它将改变星星的颜色,但不会改变部分星形填充。根据我的经验,您还需要设置 secondaryProgressTint 以阻止默认星形色调抬起丑陋的头。这是我的代码:
android:progressTint="@color/accent"
android:secondaryProgressTint="@android:color/transparent"
Hope this helps someone in my situation.
希望这对我这种情况的人有所帮助。
回答by Tony Augustine
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/RatingBar"/>
with theme in styles
以风格为主题
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/gray</item>
<item name="colorControlActivated">@color/yellow</item>
回答by Marina Tello Colado
When I did it, and I had to put the TintMode too.
当我这样做时,我也必须放置 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"/>
only for api version 21 and above
仅适用于 21 及更高版本的 api
回答by Navadip Patel
The rating bar is used automatically at run time for change color on touch star.
评级栏在运行时自动用于更改触摸星上的颜色。
First add style in app\src\main\res\values\styles.xml file:
首先在 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>
Then your rating bar add theme like this:
然后你的评分栏添加这样的主题:
<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"/>