How to make RatingBar to show five stars

前端 未结 18 477
栀梦
栀梦 2020-12-04 18:58

I am following the standard example of how to add a RatingBar. To control the number of stars I tried to use android:numStars=\"5\". The problem is

相关标签:
18条回答
  • 2020-12-04 19:26

    For me, I had an issue until I removed the padding. It looks like there is a bug on certain devices that doesn't alter the size of the view to accommodate the padding, and instead compresses the contents.

    Remove padding, use layout_margin instead.

    0 讨论(0)
  • 2020-12-04 19:26

    If you are wrapping the RatingBar inside a ConstraintLayout with match_constraint for its width, the editor preview is going to show a number of stars proportional to its actual width, no matter what if you set android:numStars property. Use wrap_content to get the correct preview:

    <RatingBar android:id="@+id/myRatingBar"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginStart="48dp"
               android:layout_marginEnd="48dp"
               android:numStars="5"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toBottomOf="parent" />
    
    0 讨论(0)
  • 2020-12-04 19:28

    You can add default rating of five stars in side the xml layout

    android:rating="5"
    

    Edit:

    <RatingBar
            android:id="@+id/rb_vvm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="@dimen/space2"
            android:layout_marginTop="@dimen/space1"
            style="@style/RatingBar"
            android:numStars="5"
            android:stepSize="1"
            android:rating="5" />
    
    0 讨论(0)
  • 2020-12-04 19:29

    I'm also facing the problem that exceeding the stars than specified no of stars. For this, we don't want worry about whatever the layout type either relative or linear layout. Simply use the width as follows:

    ratingBar.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    


    Please avoid using match_parent and fill_parent for ratingbar.
    Hope the things would be helpful.

    0 讨论(0)
  • 2020-12-04 19:30

    The actual thing over here is to use wrap_content instead of match_parent. If you will use match_parent the layout will be filled with the stars even if it is more than the numStars variable.

    0 讨论(0)
  • 2020-12-04 19:32

    Additionally, if you set a layout_weight, this supersedes the numStars attribute.

    0 讨论(0)
提交回复
热议问题