我是一名 android 初学者,我想制作我的自定义 ratingBar。
免责声明:它不是重复的。因为我读过的所有帖子都询问如何更改星星的颜色以及如何删除笔触。那不是我想要的。我想要描边并且能够改变边框的颜色。
空白时使用透明和黄色的笔触,黄色背景和笔触表示一半和填充。
我不想使用 png。我已经有自己的了,但它们太小了。我只是不想让设计师制作新的,如果我可以只使用 XML 属性和 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文件中写了代码
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
[所以现在它有灰色背景,黄色表示填充,现在没有笔划。
我已经知道如何将背景设置为透明并使其变黄。我只是不知道如何设置笔触颜色。非常感谢!!!!
最佳答案
将所有可绘制颜色设置为黄色(在您的情况下 getResources().getColor(R.color.gold)
for all)设置笔触和背景。 (用于背景、次要进度和进度)
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);
从 RatingBar
中删除 progressBackgroundTint
,progressTint
,secondaryProgressTint
属性不需要它。
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.5"/>
结果是::
希望这会有所帮助(粉红色只是背景色)
将可绘制对象 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);
关于java - 如何设置android评分栏笔划的颜色? (不是星星的颜色,而是边界的颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35378098/