我已将ratingBar颜色设置为与Android的默认蓝色和灰色不同的颜色 - 如果选中它们,我使用黑色背景和粉色制作了我的星星。Android RatingBar:无法填充部分ratingBar如果我定制它的颜色
这是我为我的mainactivity代码:
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);
}
}
你可以看到,如果我把在等级为3.6,它选择最多4
但是,如果我将我自己的自定义颜色注释掉了,ratingBar自行设置正确,只显示第四颗星的一部分,因为传递给它的值为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未正确填入自己?
我创建了一个小的回购,你可以下载它,并在这里进行测试:https://github.com/Winghin2517/RatingBarTest2
编辑:
FlyingPumba图书馆显示了什么,我想实现一些希望,但是我需要一个风格,其中星星背景在屏幕上出现时为白色,当用户按下星号时,它将以纯色填充。就我而言,我希望它充满绿色。
以下是我所能达到的,但还没有完成。我改变了滚动视图的背景,其中的星星坐着粉红色,以便你可以看到,而不是星星的白色背景,来自scrollview背景的粉红色闪耀。对于我的使用案例,我希望白色星星坐在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"
/>
我在库中添加了一些功能,可以让你完成你想要的功能(查看我添加到答案中的示例布局)。 – FlyingPumba