如何设置android评级栏的笔画颜色? (不是星星的颜色,而是BORDER) [英] How can I set the color of android rating bar's stroke? (Not the color of the stars but the BORDER)
问题描述
我是一个Android初学者,我想制作我的自定义评级栏。
I am an android beginner and I want to make my custom ratingBar.
免责声明:这不是重复。因为我读过的所有帖子都询问如何更改星星的颜色以及如何删除笔画。那不是我想要的。我想要中风并且能够改变边框的颜色。
Disclaimer: it's not a duplicate. because all the posts I have read asks about how to change colors of the star and how to remove the stroke. That is NOT what I want. I want to have the stroke and to be able to change the color of the border.
透明黄色笔划,空白,黄色背景和笔划为一半和填充。
With a transparent and yellow stroke while empty, and yellow background and stroke for half and filled.
我不想使用png。我已经拥有自己的但是它们太小了。如果我只使用XML属性和drawables制作星星,我只是不想让设计师制作新的。
I do NOT want to use pngs. I have my own already but they are too small. I just dont want to ask the designer to make new ones if I can make the stars using only XML attributes and drawables.
<RatingBar
android:id="@+id/thread_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:progressBackgroundTint="@color/gray"
android:progressTint="@color/gold"
android:rating="2.5"
android:secondaryProgressTint="@color/gray"
android:stepSize="0.5"
style="?android:attr/ratingBarStyleSmall"
/>
我和另一位工程师一起工作。他还在java文件中编写代码
I work on this with another engineer. He also wrote code in a java file
private void setRatingBarStart(RatingBar rating_bar) {
LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();
stars.getDrawable(2)
.setColorFilter(getResources().getColor(R.color.gold),
PorterDuff.Mode.SRC_ATOP); // for filled stars
stars.getDrawable(1)
.setColorFilter(getResources().getColor(R.color.light_gray),
PorterDuff.Mode.SRC_ATOP); // for half filled stars
stars.getDrawable(0)
.setColorFilter(getResources().getColor(R.color.light_gray),
PorterDuff.Mode.SRC_ATOP); // for empty stars
[所以现在它有一个灰色背景,黄色表示填充,没有描边现在。
[So now it has a grey background, yellow for filled, and NO stroke now.
我希望它具有透明度背景,黄色描边和黄色填充。
看起来这个
And I want it to have transparent background, yellow stroke and yellow for filled. Looks like this
我已经知道如何设置背景透明并使其成为可能黄色。我只是不知道如何设置笔触颜色。非常感谢!!!!
I already know how to set the background transparent and to make it yellow. I just dont know how to set the stroke color. Thanks a lot!!!!
推荐答案
将所有可绘制的颜色都设置为黄色(在您的情况下 getResources()。getColor(R.color.gold)
for all )用于设置笔划和背景。 (用于后台,次要进度和进度)
Put all drawable color to Yellow(in your case getResources().getColor(R.color.gold)
for all) for set strokes and background. (for background,secondary progress and progress)
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
remove progressBackgroundTint
, progressTint
, secondaryProgressTint
属性来自 RatingBar
不需要它。
remove progressBackgroundTint
,progressTint
,secondaryProgressTint
attributes from RatingBar
no need of it.
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.5"/>
,结果是::
希望这有助于(粉红色只是背景色)
将drawable 1和0设置为红色后:
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP);
这篇关于如何设置android评级栏的笔画颜色? (不是星星的颜色,而是BORDER)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!