繁体   English   中英

无法更改Android RatingBar的星色

[英]Cannot change Android RatingBar's star color

AndroidBar星形颜色的默认颜色是蓝色,如下所示: 在此处输入图片说明

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

使用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的ASUS手机。 还有另一种方法可以在Android 4及更高版本上使用吗? 谢谢。

您需要在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"/>

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM