如何设置android评级栏的笔画颜色?(不是星星的颜色,而是BORDER)

hus*_*ove 29 java xml android drawable ratingbar

我是一个Android初学者,我想制作我的自定义ratingBar.

免责声明:这不是重复.因为我读过的所有帖子都询问如何更改星星的颜色以及如何删除笔画.那不是我想要的.我想要中风,并能够改变边框的颜色.

具有透明和黄色的笔划,同时为空,黄色背景和笔划为一半并填充.

我不想使用png.我已经拥有自己的但是它们太小了.如果我只使用XML属性和drawables制作星星,我只是不想让设计师创建新的.

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

我和另一位工程师一起工作.他还在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
Run Code Online (Sandbox Code Playgroud)

[所以现在它有一个灰色背景,黄色填充,现在没有中风.

在此输入图像描述

我希望它有透明背景,黄色笔触和黄色填充.看起来像这样 在此输入图像描述

我已经知道如何设置背景透明并使其变黄.我只是不知道如何设置笔触颜色.非常感谢!!!!

KDe*_*kar 19

将所有可绘制颜色设置为黄色(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); 
Run Code Online (Sandbox Code Playgroud)

除去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"/>
Run Code Online (Sandbox Code Playgroud)

结果是::

此搜索

希望这会有所帮助(粉红色只是背景色)

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

图像2

  • 我是唯一一个接受颜色填充而不是中风的人吗? (10认同)

Dax*_*iya 7

不要使用任何库或样式.使用波纹管步骤得到结果.

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

然后在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"
    />
Run Code Online (Sandbox Code Playgroud)

  • 我知道我的问题 - 我使用Vector,我应该用PNG星替换它 (2认同)

Yoa*_*uet 5

我想我真的找到了你需要的东西。您必须RatingBar按照本答案中的说明创建自定义,但您必须使用矢量可绘制对象而不是标准对象。

您可以在此网站上下载 3 个可绘制对象(完整、一半和空):

https://materialdesignicons.com/

或者右键单击您的res文件夹:--> 新建--> Android Studio 上的矢量资产。

这是 3 个可绘制的 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,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" />
Run Code Online (Sandbox Code Playgroud)

半星:

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

空星:

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

要更改它们的大小,只需更新属性heightwidth.

要更改它们的颜色,只需更新属性fillColor并设置黄色。