Java 如何设置分级栏的星形颜色?
如何设置分级栏的星形颜色?我想要黄色的星星。您是否尝试过xml主题,例如Java 如何设置分级栏的星形颜色?,java,android,colors,ratingbar,Java,Android,Colors,Ratingbar,如何设置分级栏的星形颜色?我想要黄色的星星。您是否尝试过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/sta
<?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" />
使用您自己的绘图工具?您确实需要三个星形图像(star\u filled\u show.png、star\u half\u show.png和star\u empty\u show.png)和一个xml,仅此而已
将这3幅图像放入res/drawable
将下面的评级barstars.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"/>
提到的博客有点复杂,我使用了类似但更简单的方法。您确实需要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"/>
就这样。试试这个
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW,PorterDuff.Mode.SRC_ATOP);
这对我很有用:
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#FFFDEC00"), PorterDuff.Mode.SRC_ATOP);
最简单的方法是:
android:progressTint="@color/color"
光滑而有光泽。我发现仅仅设置progressTint并不是一个完整的解决方案。如果步长小于1,它将更改星形的颜色,但不会更改部分星形填充。根据我的经验,您还需要设置secondaryProgressTint,以阻止默认的星形色调抬起它丑陋的头。这是我的密码:
android:progressTint="@color/accent"
android:secondaryProgressTint="@android:color/transparent"
希望这对我这种情况的人有所帮助。
<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>
@颜色/灰色
@颜色/黄色
当我这么做的时候,我也不得不把色彩模式
<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及更高版本在运行时自动使用分级栏在touch star上更改颜色 首先在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"/>
尝试以下方法:。如果您正在使用AppCompat activity Hi kwishnu,请找到最简单的方法。我尝试过此解决方案,但当我尝试将评级设置为1.1、1.2等时。。。它显示为1.5。我拍了三张星星的照片,一张是空的,一张是半满的,一张是满的。我想应该在它们之间插值得到分数分量,我只是想知道是否有其他人也有同样的问题,或者我做了一些明显错误的事情?我喜欢这个答案,因为你不需要定制星星。我只需要改变颜色,因为问题不是星星。唯一的缺点是单一颜色,我想其中一种可能需要修改其他可绘制的颜色well@suku是的,但是这个问题没有提到关于minSDK的任何限制。你必须为空星设置android:progressBackgroundTint
。属性progressTint
和secondaryProgressTint
仅在API级别21及更高的版本中使用。@ViliusK什么是使这适用于所有API版本的最佳方法那星星半满的时候呢?我希望星星是半黄半灰的——或者别的什么我不太清楚,但是你可以用渐变或者porterduff模式