【问题标题】:Change (only) the color of RatingBar(仅)更改 RatingBar 的颜色
【发布时间】:2016-11-13 14:48:58
【问题描述】:

我正在尝试将星星的颜色更改为黄色。
我不想使用其他样式(!),只是更改 RatingBar 中内置星星的颜色。

我已经阅读了一些如何更改样式的教程,它们都非常相似。但由于某种原因,它对我不起作用。
无论如何,由于我只想更改颜色,而不是整个样式,因此我使用了以下代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        rbBookRating = (RatingBar) findViewById(R.id.rbRating);
        rbBookRating.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {

            }
        });
        LayerDrawable stars = (LayerDrawable) rbBookRating.getProgressDrawable();
        stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(1).setColorFilter(Color.CYAN, PorterDuff.Mode.SRC_ATOP);
        stars.getDrawable(0).setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
    }

布局

<RatingBar
android:id="@+id/rbRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isIndicator="true"
android:stepSize="0.5" />

(似乎)我的应用程序在此行之后立即崩溃:

LayerDrawable stars = (LayerDrawable) rbBookRating.getProgressDrawable();

有什么建议吗?

【问题讨论】:

  • 你能添加你看到的logcat错误吗?
  • 请添加logcat @Nick

标签: android android-studio ratingbar


【解决方案1】:

我只需要使用:

DrawableCompat.setTint(rbBookRating.getProgressDrawable(), Color.YELLOW);

还是谢谢...

【讨论】:

    【解决方案2】:

    首先创建一个主题

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

    然后将其添加到您的 xml 中

    <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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多