更改 android 中的评分栏颜色

Change ratingbar color in android

我在我的 android 项目中实现了 RatingBar,它工作得很好,但我想更改 ratingbar 填充颜色,现在默认它填充绿色,但我想更改为另一种颜色, 下面我附上了我的评分栏图片,请任何人帮助我如何更改评分栏的颜色,谢谢

 http://i.stack.imgur.com/3cN13.png


 **xml code:**

 <RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/ratingAndReviewtext"
    android:isIndicator="?android:attr/ratingBarStyleIndicator"
    android:numStars="5"
    android:rating="0.0"
    android:stepSize="0.0" />

Step #1: Create your own style, by cloning one of the existing styles (from $ANDROID_HOME/platforms/$SDK/data/res/values/styles.xml), putting it in your own project's styles.xml, and referencing it when you add the widget to a layout.

Step #2: Create your own LayerDrawable XML resources for the RatingBar, pointing to appropriate images to use for the bar. The original styles will point you to the existing resources that you can compare with. Then, adjust your style to use your own LayerDrawable resources, rather than built-in ones.

参考:


评分栏示例和教程:http://www.skholingua.com/android-basic/user-interface/form-widgets/ratingbar

以编程方式尝试以下代码

 RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
    stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);