【问题标题】:Rating Bar Fill color android评分栏填充颜色android
【发布时间】:2016-06-08 14:03:39
【问题描述】:

在评分栏中填充颜色以进行十进制评分对于低于 23 的 API 可以正常工作,而在 API 23 中则无法正常工作。

代码如下:

RatingBar ratingBar = (RatingBar) view.findViewById(R.id.ratingbar);
ratingBar.setNumStars(5);
ratingBar.setMax(5);
ratingBar.setStepSize(0.1f);
ratingBar.setRating(0.5);
Drawable ratingBarColor = ratingBar.getProgressDrawable();
DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

这将用红色填充 0.5(half) 星的颜色。 但是对于 android API 23(MarshMallow),无论你设置什么评级,它都会填满所有星星。我能知道导致这个问题的 API 23 发生了什么变化吗?或者让我知道这段代码的问题。

用于测试的手机 (6.0):HTC One M8、Moto X Play、Nexus 6。 5.1.1:Moto X(第一代)、Nexus 4、Moto G1、G2

编辑:我通过删除 API23 中的 setTint 进行了测试,该 API23 设置了带有十进制评级的白色。因此,DrawableCompat 设置十进制评级星的颜色可能会出现问题。

【问题讨论】:

  • 尝试过给予风格? style="?android:attr/ratingBarStyle"??
  • @Viren 我正在以编程方式设置自定义评分栏。
  • 我没听懂你。您正在创建自定义样式???我告诉你在你放置评级栏的 xml 中声明它。
  • @Viren 谢谢。有什么不同吗?。
  • 希望如此。请放一次。

标签: android ratingbar


【解决方案1】:

对于 API 23(MarshMallow),RatingBar 确实会遇到所有星星都改变颜色的问题(无论是 1 星还是 5 星),看起来总是 5 星。

更新答案: 以下代码有效

if (ratingBar.getProgressDrawable() instanceof LayerDrawable) {
    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    DrawableCompat.setTint(stars.getDrawable(2), color);
}
else {
    // for Android 4.3, ratingBar.getProgressDrawable()=DrawableWrapperHoneycomb
    DrawableCompat.setTint(ratingBar.getProgressDrawable(), color);
}

以下内容不适用于 Mashmallow

Drawable ratingBarColor = ratingBar.getProgressDrawable();
DrawableCompat.setTint(ratingBarColor, ContextCompat.getColor(getActivity(), R.color.red));

在布局文件中记得使用 android.support.v7.widget.AppCompatRatingBar,而不是 RatingBar。

【讨论】:

  • 感谢@Desmond 的回答。我会试试看。
【解决方案2】:

您可以制作自定义评分栏

1 只需在您的可绘制文件夹中创建 xml 文件并将其命名为 rating_bar.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/rating_empty" />
        <item android:id="@android:id/secondaryProgress" android:drawable="@drawable/rating_empty" />
        <item android:id="@android:id/progress" android:drawable="@drawable/rating_filled" />
    </layer-list>

2 制作另一个可绘制的 xml 文件并将其命名为 rating_empty.xml

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_unrated_star" />

<item android:drawable="@drawable/ic_unrated_star" />

3 为填充星创建另一个可绘制的 xml 文件并将其命名为 rating_filled.xml

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/ic_rated_star" />

<item android:drawable="@drawable/ic_rated_star" />

4 转到您的值文件夹并打开 style.xml 并添加样式

<style name="customRatingBar" parent="@android:style/Widget.RatingBar">
        <item name="android:progressDrawable">@drawable/rating_bar</item>
    </style>

5 只需使用您刚刚创建的自定义评分栏

    <RatingBar
    style="@style/customRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:numStars="5" />

注意ic_rated_staric_unrated_star完全是实心星空星图像,您想在评分栏中显示。

希望这对您有用...祝您好运...

【讨论】:

  • 如何根据条件更改评分栏的颜色?请在回答前阅读问题。
猜你喜欢
  • 2012-04-24
  • 2023-04-10
  • 1970-01-01
  • 2010-10-29
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
相关资源
最近更新 更多