java 如何设置android评级栏的笔触颜色?(不是星星的颜色而是BORDER)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35378098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set the color of android rating bar's stroke? (Not the color of the stars but the BORDER)
提问by husky love
I am an android beginner and I want to make my custom ratingBar.
我是一个 android 初学者,我想制作我的自定义 ratingBar。
Disclaimer: it's not a duplicate. because all the posts I have read asks about how to change colors of the star and how to remove the stroke. That is NOT what I want. I want to have the stroke and to be able to change the color of the border.
免责声明:它不是重复的。因为我读过的所有帖子都询问如何更改星星的颜色以及如何删除笔划。那不是我想要的。我想要描边并能够更改边框的颜色。
With a transparent and yellow stroke while empty, and yellow background and stroke for half and filled.
空时有透明和黄色的描边,黄色背景和描边为一半和填充。
I do NOT want to use pngs. I have my own already but they are too small. I just dont want to ask the designer to make new ones if I can make the stars using only XML attributes and drawables.
我不想使用 png。我已经有了自己的,但它们太小了。如果我只能使用 XML 属性和可绘制对象来制作星星,我只是不想要求设计师制作新的。
<RatingBar
android:id="@+id/thread_rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:progressBackgroundTint="@color/gray"
android:progressTint="@color/gold"
android:rating="2.5"
android:secondaryProgressTint="@color/gray"
android:stepSize="0.5"
style="?android:attr/ratingBarStyleSmall"
/>
I work on this with another engineer. He also wrote code in a java file
我和另一位工程师一起研究这个。他还在java文件中写了代码
private void setRatingBarStart(RatingBar rating_bar) {
LayerDrawable stars = (LayerDrawable) rating_bar.getProgressDrawable();
stars.getDrawable(2)
.setColorFilter(getResources().getColor(R.color.gold),
PorterDuff.Mode.SRC_ATOP); // for filled stars
stars.getDrawable(1)
.setColorFilter(getResources().getColor(R.color.light_gray),
PorterDuff.Mode.SRC_ATOP); // for half filled stars
stars.getDrawable(0)
.setColorFilter(getResources().getColor(R.color.light_gray),
PorterDuff.Mode.SRC_ATOP); // for empty stars
[So now it has a grey background, yellow for filled, and NO stroke now.
[所以现在它有一个灰色背景,黄色表示填充,现在没有描边。
And I want it to have transparent background, yellow stroke and yellow for filled.
Looks like this
I already know how to set the background transparent and to make it yellow. I just dont know how to set the stroke color. Thanks a lot!!!!
我已经知道如何将背景设置为透明并使其变黄。我只是不知道如何设置笔触颜色。非常感谢!!!!
回答by KDeogharkar
Put all drawable color to Yellow(in your case getResources().getColor(R.color.gold)
for all) for set strokes and background. (for background,secondary progress and progress)
将所有可绘制颜色设置为黄色(在您的情况下getResources().getColor(R.color.gold)
为所有)以设置笔触和背景。(用于背景,次要进度和进度)
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
remove progressBackgroundTint
,progressTint
,secondaryProgressTint
attributes from RatingBar
no need of it.
删除progressBackgroundTint
, progressTint
,不需要它的secondaryProgressTint
属性RatingBar
。
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:rating="2.5"/>
and result is ::
结果是::
Hope this will help (pink color is just background color)
希望这会有所帮助(粉红色只是背景色)
after set drawable 1 and 0 to red:
将 drawable 1 和 0 设置为红色后:
RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);
LayerDrawable stars = (LayerDrawable) ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(0).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP);
stars.getDrawable(1).setColorFilter(Color.Red, PorterDuff.Mode.SRC_ATOP);
回答by Daxesh Vekariya
Don't use any library or style for that. Use bellow step you get the result.
不要为此使用任何库或样式。使用下面的步骤你会得到结果。
create a file in drawable folder as custom_ratingbar.xml
在 drawable 文件夹中创建一个文件作为 custom_ratingbar.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/star_empty" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/star_empty" />
<item android:id="@android:id/progress"
android:drawable="@drawable/star_full" />
</layer-list>
then use following in your RatingBar :
然后在您的 RatingBar 中使用以下内容:
<RatingBar
android:id="@+id/ratingBarChef"
style="?android:attr/ratingBarStyleSmall"
android:numStars="5"
android:stepSize="0.2"
android:rating="3.0"
android:progressDrawable="@drawable/custom_ratingbar"
android:isIndicator="false"
/>
回答by Yoann Hercouet
I think I actually found what you need. You will have to create a custom RatingBar
as explained in this answerBUT you will have to use vector drawables and not standard ones.
我想我真的找到了你需要的东西。您必须RatingBar
按照本答案中的说明创建自定义,但您必须使用矢量可绘制对象而不是标准对象。
You can download the 3 drawables (full, half and empty) on this website:
您可以在此网站上下载 3 个可绘制对象(完整、一半和空):
https://materialdesignicons.com/
https://materialdesignicons.com/
Or by doing a right click on your res
folder: --> New --> Vector asset on Android Studio.
或者右键单击您的res
文件夹:--> 新建--> Android Studio 上的矢量资产。
Here are the 3 drawables XML in case you don't find them:
这是 3 个可绘制的 XML,以防您找不到它们:
Full star:
满星:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z" />
Half star:
半星:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M12,15.89V6.59L13.71,10.63L18.09,11L14.77,13.88L15.76,18.16M22,9.74L14.81,9.13L12,2.5L9.19,9.13L2,9.74L7.45,14.47L5.82,21.5L12,17.77L18.18,21.5L16.54,14.47L22,9.74Z" />
Empty star:
空星:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M12,15.39L8.24,17.66L9.23,13.38L5.91,10.5L10.29,10.13L12,6.09L13.71,10.13L18.09,10.5L14.77,13.38L15.76,17.66M22,9.24L14.81,8.63L12,2L9.19,8.63L2,9.24L7.45,13.97L5.82,21L12,17.27L18.18,21L16.54,13.97L22,9.24Z" />
To change their size, just update the attributes height
and width
.
要更改它们的大小,只需更新属性height
和width
.
To change their color, just update the attribute fillColor
and set your yellow color.
要更改它们的颜色,只需更新属性fillColor
并设置黄色。
回答by Daxesh Vekariya
One Another way is create custom rating bar if you don't have any images follow below steps. This all file create in drawable folder.
另一种方法是创建自定义评分栏,如果您没有任何图像,请按照以下步骤操作。这所有文件都在可绘制文件夹中创建。
emptystar.xml
空之星.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#F1C332" android:pathData="M12,15.39L8.24,17.66L9.23,13.38L5.91,10.5L10.29,10.13L12,6.09L13.71,10.13L18.09,10.5L14.77,13.38L15.76,17.66M22,9.24L14.81,8.63L12,2L9.19,8.63L2,9.24L7.45,13.97L5.82,21L12,17.27L18.18,21L16.54,13.97L22,9.24Z" />
</vector>
full_star.xml
full_star.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#F1C332" android:pathData="M12,17.27L18.18,21L16.54,13.97L22,9.24L14.81,8.62L12,2L9.19,8.62L2,9.24L7.45,13.97L5.82,21L12,17.27Z" />
</vector>
half_star.xml
half_star.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M12,15.89V6.59L13.71,10.63L18.09,11L14.77,13.88L15.76,18.16M22,9.74L14.81,9.13L12,2.5L9.19,9.13L2,9.74L7.45,14.47L5.82,21.5L12,17.77L18.18,21.5L16.54,14.47L22,9.74Z" >
</path>
</vector>
food_ratingbar_full_empty.xml
food_ratingbar_full_empty.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/empty_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/empty_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/empty_star" />
<item android:drawable="@drawable/empty_star" />
food_ratingbar_full_filled.xml
food_ratingbar_full_filled.xml
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="true"
android:state_window_focused="true"
android:drawable="@drawable/full_star" />
<item android:state_focused="true"
android:state_window_focused="true"
android:drawable="@drawable/full_star" />
<item android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/full_star" />
<item android:drawable="@drawable/full_star" />
food_rating_bar.xml
food_rating_bar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background"
android:drawable="@drawable/food_ratingbar_full_empty" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/food_ratingbar_full_empty" />
<item android:id="@android:id/progress"
android:drawable="@drawable/food_ratingbar_full_filled" />
style.xml
样式文件
<style name="foodRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/food_rating_bar_full</item>
<item name="android:minHeight">23dip</item>
<item name="android:maxHeight">25dip</item>
</style>
In your layout
在您的布局中
<RatingBar
android:id="@+id/ratingBarDish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp10"
android:layout_marginStart="@dimen/dp10"
style="@style/foodRatingBar"
android:isIndicator="true" />
回答by Muhammad Haroon
hi try to use this and use border image as a star https://github.com/ome450901/SimpleRatingBaras icon_starempty and icon_starfilled are your respective images
嗨尝试使用它并使用边框图像作为星星https://github.com/ome450901/SimpleRatingBar因为 icon_starempty 和 icon_starfilled 是你各自的图像
<com.willy.ratingbar.ScaleRatingBar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/deals_ratingbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/_10dp"
android:layout_toRightOf="@+id/txt_newprice"
android:layout_weight=".4"
android:gravity="center|right"
android:outlineProvider="none"
app:srb_drawableEmpty="@drawable/icon_starempty"
app:srb_drawableFilled="@drawable/icon_starfilled"
app:srb_isIndicator="true"
app:srb_numStars="5"
app:srb_starHeight="@dimen/_10dp"
app:srb_starPadding="@dimen/_3dp"
app:srb_starWidth="@dimen/_10dp" />