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

Sim*_*mon 10 android ratingbar

我已将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);
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以看到,如果我将评级设为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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

为什么如果我创建自己的自定义颜色,ratingBar不能正确填充?

我创建了一个小型仓库,你可以在这里下载并测试它: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"
    />
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Ric*_*ier 4

您正在尝试的事情走在正确的轨道上。您做错的唯一一件事就是为第二个可绘制对象分配了错误的颜色。

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

下面是更正后的代码,其工作原理如下。我对其进行了稍微重新格式化以突出显示所需的更改。

    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));
Run Code Online (Sandbox Code Playgroud)

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

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

我不打算进一步详细介绍,因为实际的 xml 代码显示在 Anantham 的回答中