RatingBar没有边框

时间:2018-01-22 12:37:12

标签: android android-layout

如何使用这样的边框制作ratingBarhttps://materialdoc.com/components/rating-bar/

我的ratingBar xml:

<RatingBar
        android:theme="@style/RatingBar"
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="2"
        android:numStars="5"
        android:stepSize="1" />

应用程序样式:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
<style name="RatingBar" parent="Theme.AppCompat">
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
</style>

这就是我得到的: enter image description here

使用滤色镜:

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.GRAY, PorterDuff.Mode.SRC_ATOP);

enter image description here

2 个答案:

答案 0 :(得分:0)

使用以下代码:

参考文献:https://stackoverflow.com/a/37135661/8448886

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); 

答案 1 :(得分:0)

解决方案:您可以将自定义绘图用于评级栏。

试试这个

XML CODE

 <RatingBar
    android:id="@+id/ratingBar"
    style="@style/MyRatingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="48dp"
    android:stepSize="0.10"   <!-- Set Stepsize as per your need -->
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText3" />

主题代码

  <style name="MyRatingBar" parent="Widget.AppCompat.RatingBar">
    <item name="android:progressDrawable">@drawable/custom_ratingbar</item>
</style>

<强> 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">
    <bitmap
        android:src="@drawable/ic_ic_star_border_blue_24dp"
        android:tint="?attr/colorControlNormal" />
</item>
<item android:id="@android:id/secondaryProgress">
    <bitmap
        android:src="@drawable/ic_ic_star_border_blue_24dp"
        android:tint="?attr/colorControlActivated" />
</item>
<item android:id="@android:id/progress">
    <bitmap
        android:src="@drawable/ic_ic_star_blue_24dp"
        android:tint="?attr/colorControlActivated" />
</item>

<强>输出:

enter image description here

enter image description here

<强>已更新

以下是素材图标https://material.io/icons/的来源。

使用此来源链接为可绘制的https://romannurik.github.io/

应用颜色

希望它有帮助..!