oja*_*jas 44 android ratingbar
我想将评级栏的颜色改为金色.
我不想自定义星星,我只想更改API 16或更高版本的颜色
我尝试了以下解决方案,但没有一个为我设计
1.
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
2.
android:progressDrawable="@color/golden" in XML
Run Code Online (Sandbox Code Playgroud)
the*_*itz 101
此选项现在包含在AppCompat库中.RatingBar的AppCompat版本会自动使用.
http://developer.android.com/reference/android/support/v7/widget/AppCompatRatingBar.html
示例(来自我自己的应用程序):
<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"/>
Run Code Online (Sandbox Code Playgroud)
主题:
<style name="RatingBar" parent="Theme.AppCompat">
<item name="colorControlNormal">@color/duskYellow</item>
<item name="colorControlActivated">@color/lightGrey</item>
</style>
Run Code Online (Sandbox Code Playgroud)
小智 18
只需将此行添加到xml(API 21及更高版本)
android:progressTint="@color/color"
Run Code Online (Sandbox Code Playgroud)
Cha*_*rma 10
正如您所提到的,您想要在不使用drawable的情况下更改星形颜色.您可以通过以下几行代码完成此操作.
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
Drawable drawable = ratingBar.getProgressDrawable();
drawable.setColorFilter(Color.parseColor("#0064A8"),PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
我的用例是为不同的评级显示不同的颜色。例如 :
1- 红色
2- 橙色
3- 黄色
4- 浅绿色
5- 深绿色
解决方案 :
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(final RatingBar ratingBar, final float rating, final boolean fromUser) {
setCurrentRating(rating);
}
});
Run Code Online (Sandbox Code Playgroud)
然后在你的setCurrentRating()方法中:
private void setCurrentRating(float rating) {
LayerDrawable drawable = (LayerDrawable)rateOverall.getProgressDrawable();
if(mContext!=null) {
switch (Math.round(rating)) {
case 1:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_red));
break;
case 2:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_orange));
break;
case 3:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_yellow));
break;
case 4:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.light_green_review));
break;
case 5:
setRatingStarColor(drawable.getDrawable(2), ContextCompat.getColor(mContext, R.color.dark_green));
break;
}
setRatingStarColor(drawable.getDrawable(1), ContextCompat.getColor(mContext, R.color.transparent));
setRatingStarColor(drawable.getDrawable(0), ContextCompat.getColor(mContext, R.color.light_grey_payment));
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的setRatingStarColor()方法中:
private void setRatingStarColor(Drawable drawable, @ColorInt int color)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
DrawableCompat.setTint(drawable, color);
}
else
{
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}
Run Code Online (Sandbox Code Playgroud)
感谢这个答案帮助我解决了这个问题。希望它可以帮助某人!
我的任务是将评级栏的颜色更改为Android > = 14 SDK的应用程序主要颜色.我在代码和xml文件中解决了这个问题.
mRatingBar = (RatingBar)view.findViewById(R.id.ratingBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
try {
Drawable progressDrawable = mRatingBar.getProgressDrawable();
if (progressDrawable != null) {
DrawableCompat.setTint(progressDrawable, ContextCompat.getColor(getContext(), R.color.colorPrimary));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
并在xml文件中
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:backgroundTint="@color/colorPrimary"
android:numStars="5"
android:progressTint="@color/colorPrimary"
android:rating="0"
android:secondaryProgressTint="@android:color/transparent"
android:stepSize="1" />
Run Code Online (Sandbox Code Playgroud)
小智 5
来自 XML
android:progressTint="#ffff8800"
Run Code Online (Sandbox Code Playgroud)
以编程方式:
Drawable drawableReview = bar.getProgressDrawable();
drawableReview.setColorFilter(Color.parseColor("#ffff8800"),
PorterDuff.Mode.SRC_ATOP);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
65629 次 |
| 最近记录: |