简体   繁体   中英

Android : Removing border in rating bar

I am using default rating bar in android and there is a weird grey border is been showing, I want to remove them.

Please Note :: Many answers on stackoverflow suggest to use own images but I don't want to, Is there is any method to remove border?

My code ::

  <RatingBar
    android:id="@+id/layout_fragment_home_recycler_view_rating_bar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"
    android:rating="4.0"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="7dp"
    android:layout_marginLeft="7dp" />

What I am getting

在此处输入图片说明

Just remove

style="?android:attr/ratingBarStyleSmall"

From your layout

Create custom style to your rating bar

<style name="RatingBarStyle" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar_selector</item>
    <item name="android:minHeight">34dp</item>
    <item name="android:maxHeight">34dp</item>
</style>

your rating bar selector

drawable/ratingbar_selector.xml

<item android:id="@+android:id/background" 
    android:drawable="@drawable/..." />

<item android:id="@+android:id/secondaryProgress"
    android:drawable="@drawable/..." />

<item android:id="@+android:id/progress"
    android:drawable="@drawable/..." />

This worked for me:

RatingBar coachRating = (RatingBar) findViewById(R.id.rcoach_rbr_rating);
LayerDrawable stars = (LayerDrawable) coachRating.getProgressDrawable();
stars.getDrawable(2).setColorFilter(ContextCompat.getColor(itemView.getContext(), 
                        R.color.colorAccent),PorterDuff.Mode.SRC_ATOP); // for filled stars
stars.getDrawable(1).setColorFilter(ContextCompat.getColor(itemView.getContext(), 
                        R.color.white), PorterDuff.Mode.SRC_ATOP); // for half filled stars
stars.getDrawable(0).setColorFilter(ContextCompat.getColor(itemView.getContext(), 
                        R.color.white), PorterDuff.Mode.SRC_ATOP); // for empty stars

Reference: How can I set the color of android rating bar's stroke? (Not the color of the stars but the BORDER)

Try this, it don't need images.

public static void setRatingStyle(RatingBar ratingbar, int ratingNormalColor, int ratingProgressColor) {
    LayerDrawable stars = (LayerDrawable) ratingbar.getProgressDrawable();
    stars.getDrawable(0).setColorFilter(ratingNormalColor, PorterDuff.Mode.SRC_ATOP);
    stars.getDrawable(2).setColorFilter(ratingProgressColor, PorterDuff.Mode.SRC_ATOP);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM