Android RatingBar:如果我自定义它的颜色,则无法填充部分 ratingBar

Android RatingBar: Cannot fill in a partial ratingBar if I customise it's color

我已将 ratingBar 颜色设置为与 Android 默认的蓝色和灰色不同的颜色 - 我将我的星星设置为黑色背景和粉色(如果它们被选中)。

这是我的主要活动代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));
        ratingBar.setIsIndicator(false);
        ratingBar.setRating(3.6f);
        ratingBar.setStepSize(0.1f);
        ratingBar.invalidate();
        ratingBar.setIsIndicator(true);
    }
}

你可以看到,如果我输入 rating 为 3.6,它最多选择 4。

但是,如果我注释掉自己的自定义颜色,则 ratingBar 会正确设置自己以仅显示填充的第 4 颗星的一部分,因为传递给它的值为 3.6:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
/*        LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
                ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
        DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
                ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));*/
        ratingBar.setIsIndicator(false);
        ratingBar.setRating(3.6f);
        ratingBar.setStepSize(0.1f);
        ratingBar.invalidate();
        ratingBar.setIsIndicator(true);
    }
}

为什么我创建自己的自定义颜色时,ratingBar 无法正确填充?

我创建了一个小的 repo,你可以在这里下载并测试它:https://github.com/Winghin2517/RatingBarTest2

编辑:

FlyingPumba 的库显示了我想要实现的一些承诺,但我需要一种样式,当它出现在屏幕上时,星星背景是白色的,当用户按下星星时,它会被纯色填充。就我而言,我希望它填充绿色。

以下是我能够实现的,但还远远不够。我将星星所在的滚动视图背景更改为粉红色,这样您就可以看到滚动视图背景中的粉红色闪耀,而不是星星的白色背景。对于我的用例,我希望白色星星位于 imageview 前面,我不希望 imageview 中的图像透过星星闪耀。

此外,我根本不需要星星的白色描边,因为我想让星星的造型保持简单。

<com.iarcuschin.simpleratingbar.SimpleRatingBar
    android:id="@+id/ratingBar3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:srb_rating="3.6"
    app:srb_fillColor="@color/material_greenA700"
    app:srb_borderColor="@color/material_white"
    app:srb_starBorderWidth="0"
    app:srb_pressedFillColor="@color/material_greenA700"
    app:srb_starSize="30dp"
    />

您只需将 DrawableCompat.wrap(..) 可绘制对象更新为 .mutate()。您的代码如下所示:

DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0).mutate()),
            ContextCompat.getColor(getApplicationContext(), android.R.color.background_dark));
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1).mutate()),
            ContextCompat.getColor(getApplicationContext(), R.color.colorAccent)); // Partial star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2).mutate()),
            ContextCompat.getColor(getApplicationContext(), R.color.colorAccent));

If the color is changed dynammicaly the drawable must be mutable

参见问题:Android: Tint using DrawableCompat

并回答:

您可以查看 Drawable mutations blog by Romain Guy and also see mutate() 方法 android 开发者文档。

希望对你有所帮助

使用股票 RatingBar 会很痛苦,你应该试试这个库:SimpleRatingBar

(我有点偏见,因为我是创作者)

它的特点:

  • 完全工作android:layout_width:可以设置为wrap_contentmatch_parent或任意dp。
  • 任意数量的星星。
  • 任意步长。
  • 星星的大小可以精确控制或通过设置最大大小来控制。
  • 正常状态下可自定义的颜色(边框、填充和星星背景)。
  • 按下状态下的可自定义颜色(边框、填充和星星背景)。
  • 星星之间可自定义的大小间隔。
  • 可自定义星星的边框宽度。
  • 可定制的星星圆角半径。
  • 允许设置 OnRatingBarChangeListener
  • 星星填充可以设置为从左到右或从右到左(RTL 语言支持)。
  • AnimationBuilder 集成在视图中以通过动画编程设置评级。

Here is a preview of it.

您可以在 jcenterMaven Central 中找到它。所以在你的 build.gradle 文件中添加你的依赖项:

compile 'com.iarcuschin:simpleratingbar:0.1.+'

编辑: 我最近添加了更改星星背景颜色(空颜色)和禁用边框的可能性。 要实现您想要的效果,您可以添加到布局中:

<com.iarcuschin.simpleratingbar.SimpleRatingBar
    android:id="@+id/ratingBar3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:srb_rating="3.6"
    app:srb_starBackgroundColor="@color/white"
    app:srb_fillColor="@color/material_greenA700"
    app:srb_borderColor="@color/material_white"
    app:srb_pressedFillColor="@color/material_greenA700"
    app:srb_starSize="30dp"
    app:srb_drawBorderEnabled="false"
    />

希望有用。

在Activity.java中:

创建时:

       RatingBar   setRatingBar = (RatingBar) findViewById(R.id.setRating);
       float rating = Float.parseFloat(listString);
      // onRatingChanged(setRatingBar, 0.0f, true);

        public void onRatingChanged(RatingBar rateBar, float rating, boolean fromUser) {
    try {
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)/ ++count));
        float userRatePoint = curRate/**20*/;

        setRatingBar.setRating(userRatePoint);
        countText = (TextView) findViewById(R.id.countText);
        countText.setText(userRatePoint + " Ratings");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后试试这个小星评级吧:

    <RatingBar
     android:id="@+id/setRating"
     style="?android:attr/ratingBarStyleSmall"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     android:layout_marginTop="3dp"
     android:isIndicator="true"
     android:numStars="5"
     android:stepSize="0.1" />
       //if you want to style please use "foodRatingBar"
       //for custom star image use  "ratingstar"

style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>

值v11/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>

值v14/styles.xml

 <resources>
   <style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingstar</item>
    <item name="android:minHeight">22dip</item>
    <item name="android:maxHeight">22dip</item>
</style>
<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>
</resources>

ratingstar.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/ratingstar_default" />
        <item android:id="@android:id/secondaryProgress"
            android:drawable="@drawable/ratingstar_default" />
        <item android:id="@android:id/progress"
            android:drawable="@drawable/ratingstar_selected" />
    </layer-list>
</resources>

您的尝试是正确的。您唯一做错的是为第二个可绘制对象分配了错误的颜色。

将第二个可绘制对象视为星星的背景,而不是前景。所以部分星星需要背景颜色,而不是星星的颜色,即在你的例子中,黑色,而不是粉红色。

下面是更正后的代码,它按描述工作。我稍微重新设置了格式以突出显示所需的更改。

    RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingbar);
    LayerDrawable layerDrawable2 = (LayerDrawable) ratingBar.getProgressDrawable();

    // No star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(0)),
            ContextCompat.getColor(getApplicationContext(),
                    android.R.color.background_dark));

    // Partial star 
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(1)),
            ContextCompat.getColor(getApplicationContext(),
                    // use background_dark instead of colorAccent
                    // R.color.colorAccent));
                    android.R.color.background_dark));

    // Custom star
    DrawableCompat.setTint(DrawableCompat.wrap(layerDrawable2.getDrawable(2)),
            ContextCompat.getColor(getApplicationContext(),
                    R.color.colorAccent));

另一种方法(在许多情况下可能更好)是在 RatingBar.

上使用适当的样式

这需要为评分栏设置自定义样式,然后定义该样式使用的图层可绘制对象以指向您自己的图像或颜色。

我不打算详细介绍,因为