Android: 将评级栏的颜色更改为金色
Android: Change color of ratingbar to golden
我想将评级栏的颜色更改为金色。
我不想自定义星星,我只想为 API 16 岁或以上
更改颜色
我尝试了以下解决方案,但 none 对我有用
1.
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
2.
android:progressDrawable="@color/golden" in XML
试试这个,
您确实需要 3 张星图(red_star_full.png、red_star_half.png 和 red_star_empty.png)和一个 xml 文件。如果你想要金色的星星,那么使用金色的星星图像。这是给红星的。
您可以将 ratingbar_color.xml 放入 res/drawable。
<?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);
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#0064A8"),PorterDuff.Mode.SRC_ATOP);
此选项现已包含在 AppCompat 库中。
自动使用 RatingBar 的 AppCompat 版本。
http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html
示例(来自我自己的应用程序):
<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"/>
主题:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/lightGrey</item>
<item name="colorControlActivated">@color/duskYellow</item>
</style>
我的任务是将评分栏的颜色更改为 Android >=14 SDK 应用的原色。只有代码和 xml 文件中的更改对我有帮助。
mRatingBar = (RatingBar)view.findViewById(R.id.ratingBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
Drawable progressDrawable = mRatingBar.getProgressDrawable();
if (progressDrawable != null) {
DrawableCompat.setTint(progressDrawable, ContextCompat.getColor(getContext(), R.color.colorPrimary));
}
} catch (Exception e) {
e.printStackTrace();
}
}
并在 xml 文件中
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:backgroundTint="@color/colorPrimary"
android:numStars="5"
android:progressTint="@color/colorPrimary"
android:rating="0"
android:secondaryProgressTint="@android:color/transparent"
android:stepSize="1" />
RatingBar ratingreview;
ratingreview=(RatingBar)v.findViewById(R.id.ratingreview);
ratingreview.setRating(Float.parseFloat(objrating));
Drawable drawable = ratingreview.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#6A9A28"), PorterDuff.Mode.SRC_ATOP);
在 API 21 及更高版本中,您可以使用
更改填充星星的颜色
android:progressTint="@color/color"
笔画颜色
android:progressBackgroundTint="@color/color"
我的用例是为不同的评级显示不同的颜色。例如:
1- 红色
2- 橙色
3- 黄色
4-浅绿色
5-深绿色
解决方案:
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
setCurrentRating(rating);
}
});
然后在您的 setCurrentRating()
方法中:
private void setCurrentRating(float rating) {
LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
if(mContext!=null) {
switch (Math.round(rating)) {
case 1:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
break;
case 2:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
break;
case 3:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
break;
case 4:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
break;
case 5:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
break;
}
setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));
}
}
然后在您的 setRatingStarColor()
方法中:
private void setRatingStarColor(Drawable drawable, @ColorInt int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
DrawableCompat.setTint(drawable, color);
}
else
{
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
感谢这个 answer 帮助我解决了这个问题。
希望对大家有帮助!
简单的解决方案,使用 AppCompatRatingBar 并使用下面的代码
ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));
我找到的最简单的解决方案是更改 color.xml 中颜色 "colorAccent" 的颜色定义。无需进一步编码,这就是魔法。
使用此代码越线
Drawable drawableReview = writeReviewRatingBar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#FFFDD433"), PorterDuff.Mode.SRC_ATOP);
来自 XML
android:progressTint="#ffff8800"
以编程方式:
Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"),
PorterDuff.Mode.SRC_ATOP);
评分栏颜色变化和大小变化:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
style = "?android:attr/ratingBarStyleIndicator"
android:progressTint="#ffff8800"
android:rating="3.5"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="@style/RatingBar" />
如果您 运行 在 kitkat 上默认显示蓝色星星而不是黄色和金色,然后添加此行:style="@style/Base.Widget.AppCompat.RatingBar.Small"
使用 Kotlin Extensions 的其他解决方案允许按评级范围设置不同的颜色:
扩展函数和属性:
var RatingBar.colorScheme: Map<Float, Int>?
get() = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>?
set(value) = setTag(R.id.rating_bar_color_scheme, value)
fun RatingBar.setColorByRating(rating: Float) {
val colorScheme = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>
colorScheme[rating]?.also { color -> setLayerColor(2, color) }
setLayerColor(1, ContextCompat.getColor(context, android.R.color.transparent))
setLayerColor(0, ContextCompat.getColor(context, android.R.color.darker_gray))
}
fun RatingBar.setLayerColor(layerIndex: Int, color: Int) {
val drawable = progressDrawable as LayerDrawable
drawable.getDrawable(layerIndex).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(it, color)
} else {
it.setColorFilter(color, PorterDuff.Mode.SRC_IN)
}
}
}
使用它:
val red = ContextCompat.getColor(requireContext(), R.color.redColor)
val yellow = ContextCompat.getColor(requireContext(), R.color.yellowColor)
val green = ContextCompat.getColor(requireContext(), R.color.greenLightColor)
rbStars?.colorScheme = mapOf(
1.0F to red,
2.0F to red,
3.0F to yellow,
4.0F to green,
5.0F to green
)
rbStars?.setColorByRating(rating)
我想将评级栏的颜色更改为金色。
我不想自定义星星,我只想为 API 16 岁或以上
更改颜色
我尝试了以下解决方案,但 none 对我有用
1.
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
2.
android:progressDrawable="@color/golden" in XML
试试这个,
您确实需要 3 张星图(red_star_full.png、red_star_half.png 和 red_star_empty.png)和一个 xml 文件。如果你想要金色的星星,那么使用金色的星星图像。这是给红星的。
您可以将 ratingbar_color.xml 放入 res/drawable。
<?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);
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#0064A8"),PorterDuff.Mode.SRC_ATOP);
此选项现已包含在 AppCompat 库中。 自动使用 RatingBar 的 AppCompat 版本。
http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html
示例(来自我自己的应用程序):
<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"/>
主题:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/lightGrey</item>
<item name="colorControlActivated">@color/duskYellow</item>
</style>
我的任务是将评分栏的颜色更改为 Android >=14 SDK 应用的原色。只有代码和 xml 文件中的更改对我有帮助。
mRatingBar = (RatingBar)view.findViewById(R.id.ratingBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
Drawable progressDrawable = mRatingBar.getProgressDrawable();
if (progressDrawable != null) {
DrawableCompat.setTint(progressDrawable, ContextCompat.getColor(getContext(), R.color.colorPrimary));
}
} catch (Exception e) {
e.printStackTrace();
}
}
并在 xml 文件中
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:backgroundTint="@color/colorPrimary"
android:numStars="5"
android:progressTint="@color/colorPrimary"
android:rating="0"
android:secondaryProgressTint="@android:color/transparent"
android:stepSize="1" />
RatingBar ratingreview;
ratingreview=(RatingBar)v.findViewById(R.id.ratingreview);
ratingreview.setRating(Float.parseFloat(objrating));
Drawable drawable = ratingreview.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#6A9A28"), PorterDuff.Mode.SRC_ATOP);
在 API 21 及更高版本中,您可以使用
更改填充星星的颜色 android:progressTint="@color/color"
笔画颜色
android:progressBackgroundTint="@color/color"
我的用例是为不同的评级显示不同的颜色。例如:
1- 红色
2- 橙色
3- 黄色
4-浅绿色
5-深绿色
解决方案:
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
setCurrentRating(rating);
}
});
然后在您的 setCurrentRating()
方法中:
private void setCurrentRating(float rating) {
LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
if(mContext!=null) {
switch (Math.round(rating)) {
case 1:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
break;
case 2:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
break;
case 3:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
break;
case 4:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
break;
case 5:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
break;
}
setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));
}
}
然后在您的 setRatingStarColor()
方法中:
private void setRatingStarColor(Drawable drawable, @ColorInt int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
DrawableCompat.setTint(drawable, color);
}
else
{
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
感谢这个 answer 帮助我解决了这个问题。 希望对大家有帮助!
简单的解决方案,使用 AppCompatRatingBar 并使用下面的代码
ratingbar.setProgressTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.colorAccent)));
我找到的最简单的解决方案是更改 color.xml 中颜色 "colorAccent" 的颜色定义。无需进一步编码,这就是魔法。
使用此代码越线
Drawable drawableReview = writeReviewRatingBar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#FFFDD433"), PorterDuff.Mode.SRC_ATOP);
来自 XML
android:progressTint="#ffff8800"
以编程方式:
Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"),
PorterDuff.Mode.SRC_ATOP);
评分栏颜色变化和大小变化:
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
style = "?android:attr/ratingBarStyleIndicator"
android:progressTint="#ffff8800"
android:rating="3.5"
android:focusable="false"
android:focusableInTouchMode="false"
android:theme="@style/RatingBar" />
如果您 运行 在 kitkat 上默认显示蓝色星星而不是黄色和金色,然后添加此行:style="@style/Base.Widget.AppCompat.RatingBar.Small"
使用 Kotlin Extensions 的其他解决方案允许按评级范围设置不同的颜色:
扩展函数和属性:
var RatingBar.colorScheme: Map<Float, Int>?
get() = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>?
set(value) = setTag(R.id.rating_bar_color_scheme, value)
fun RatingBar.setColorByRating(rating: Float) {
val colorScheme = getTag(R.id.rating_bar_color_scheme) as Map<Float, Int>
colorScheme[rating]?.also { color -> setLayerColor(2, color) }
setLayerColor(1, ContextCompat.getColor(context, android.R.color.transparent))
setLayerColor(0, ContextCompat.getColor(context, android.R.color.darker_gray))
}
fun RatingBar.setLayerColor(layerIndex: Int, color: Int) {
val drawable = progressDrawable as LayerDrawable
drawable.getDrawable(layerIndex).also {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
DrawableCompat.setTint(it, color)
} else {
it.setColorFilter(color, PorterDuff.Mode.SRC_IN)
}
}
}
使用它:
val red = ContextCompat.getColor(requireContext(), R.color.redColor)
val yellow = ContextCompat.getColor(requireContext(), R.color.yellowColor)
val green = ContextCompat.getColor(requireContext(), R.color.greenLightColor)
rbStars?.colorScheme = mapOf(
1.0F to red,
2.0F to red,
3.0F to yellow,
4.0F to green,
5.0F to green
)
rbStars?.setColorByRating(rating)