无法改变Android RatingBar的明星颜色

时间:2018-03-31 17:00:34

标签: android ratingbar

AndroidBar星色的默认颜色为蓝色,如下所示: enter image description here

我想将颜色更改为绿色。我尝试了几种方法,但都没有。

使用 android:progressTint

<RatingBar
     android:id="@+id/rb"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:isIndicator="false"
     style="?android:attr/ratingBarStyleSmall"
     android:numStars="5"
     android:stepSize="1"
     android:progressTint="@color/green"
     android:layout_gravity="center"/>

另一种尝试。在styles.xml中添加一个新样式:

<style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/iron</item>
    <item name="colorControlActivated">@color/green</item>
</style>

并应用它:

<RatingBar
     android:id="@+id/rb"
     android:theme="@style/RatingBar"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:isIndicator="false"
     style="?android:attr/ratingBarStyleSmall"
     android:numStars="5"
     android:stepSize="1"
     android:layout_gravity="center"/>

我的测试设备是华硕手机,运行Android 4.4.2。还有其他方法,适用于Android 4及更新版本吗?谢谢。

2 个答案:

答案 0 :(得分:0)

您需要在values和v21下的styles.xml文件中创建两个样式。

将此样式放在values / styles.xml文件中

 <style name="RatingBar" parent="@android:style/Widget.Holo.Light.RatingBar">
        <item name="colorControlNormal">@color/colorAccent</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>

将此样式放在v21 / styles.xml文件中

<style name="RatingBar" parent="android:style/Widget.Material.RatingBar">
        <item name="colorControlNormal">@color/colorAccent</item>
        <item name="colorControlActivated">@color/colorAccent</item>
    </style>

在布局文件中,使用如下所示的样式:

<RatingBar
     android:id="@+id/rb"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:isIndicator="false"
     style="@style/RatingBar"
     android:numStars="5"
     android:stepSize="1"
     android:layout_gravity="center"/>

答案 1 :(得分:0)

style="?android:attr/ratingBarStyleSmall"替换为style="@style/RatingColor"styles.xml中,添加以下内容:

<style name="RatingColor" parent="Theme.AppCompat">
  <item name="colorControlNormal">@color/blue</item>
  <item name="colorControlActivated">@color/green</item>
</style>