【问题标题】:RatingBar without borderRatingBar 无边框
【发布时间】:2018-01-22 12:37:11
【问题描述】:

如何使ratingBar 带有这样的边框:https://materialdoc.com/components/rating-bar/

我的ratingBarxml:

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

这是我得到的:

带滤色镜:

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

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    解决方案:您可以使用自定义drawable来评分栏。

    试试这个

    XML 代码

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

    输出:

    更新

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

    使用此源链接为您的可绘制对象应用颜色https://romannurik.github.io/

    希望对您有所帮助..!

    【讨论】:

    • Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4: &lt;bitmap&gt; requires a valid 'src' attribute
    • 我猜它不支持矢量绘图
    【解决方案2】:

    使用下面的代码:

    参考: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); 
    

    【讨论】:

    • Android 设备的默认边框,因为您必须使用自定义评分栏或任何库。@Mike
    猜你喜欢
    • 2014-03-26
    • 2015-08-14
    • 2017-05-27
    • 2011-03-27
    • 2012-11-22
    • 2011-11-02
    • 2016-01-01
    • 1970-01-01
    • 2011-08-17
    相关资源
    最近更新 更多