我已将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);
}
}
您可以看到,如果我将评级设为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不能正确填充?
我创建了一个小型仓库,您可以下载并在此处进行测试: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"
/>
答案 0 :(得分:4)
你正走在正确的轨道上。你唯一做错的就是为第二个drawable分配错误的颜色。
将第二个drawable想象为星星的背景,而不是前景。所以部分星星需要背景颜色,而不是星星的颜色,例如在你的例子中,黑色,而不是粉红色。
以下是更正后的代码,其工作原理如上所述。我稍微重新格式化了以突出显示所需的更改。
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
上使用适当的样式,并且在许多情况下可能更好。
这需要为评级栏设置自定义样式,然后定义该样式使用的图层可绘制对象以指向您自己的图像或颜色。
我不打算进一步详细说明,因为the actual xml code is shown in Anantham's answer。
答案 1 :(得分:1)
在Activity.java中:
oncreate:
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>
答案 2 :(得分:-1)
您只需要使用DrawableCompat.wrap(..)
更新.mutate()
drawable。您的代码如下所示:
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));
如果动态改变颜色,则drawable必须可变
参见问题:Android: Tint using DrawableCompat
并回答: https://stackoverflow.com/a/30928051/2949612
您可以查看Romain Guy的Drawable mutations博客,另请参阅mutate()方法android开发者文档。
我希望它会帮助你。
答案 3 :(得分:-1)
使用股票评级栏可能非常痛苦,您应该尝试使用此库:SimpleRatingBar。
(我有点偏颇,因为我是创作者)
它的特点:
android:layout_width
:可以将其设置为wrap_content
,match_parent
或abritary dp。您可以在jcenter
或Maven 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"
/>
我希望它有用。