如何让分级条显示五颗星

我遵循的标准例子,如何添加一个 RatingBar。为了控制恒星的数量,我尝试使用 android:numStars="5"。问题是恒星的数量似乎根本不起作用。在人像布局我得到6颗星,当我翻转手机我得到大约10颗星。我试图在我的 Activity (myBar.setNumStars(5))中设置加载 xml 的星号数,但是这个选项也没有成功。

所以我的问题是,我如何定义我的布局,使它只显示五颗星?设置 numStars似乎不起作用。

先说声谢谢, 罗兰

124126 次浏览
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:numStars="5"
android:stepSize="0.1"
android:isIndicator="true" />

in code

mRatingBar.setRating(int)

The default value is set with andoid:rating in the xml layout.

Even i was facing the issue @Roland , I had included one more attribute called

android:layout_alignParentRight="true"

in my RatingBar declaration in XML. This attribute prevented from setting of the stars required and setting up the NumStars

Keep posted on the issues you come across !

The RatingBar.setNumStar documentation says:

Sets the number of stars to show. In order for these to be shown properly, it is recommended the layout width of this widget be wrap content.

So setting the layout width to wrap_content should solve this problem.

This worked for me: RatingBar should be inside LinearLayout other than having layout width set to wrap content for RatingBar.

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

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >


<RatingBar
android:id="@+id/ruleRatingBar"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.5"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

I found that the RatingBar stretched to a maximum number of stars because it was enclosed in a Table with the attribute android:stretchColumns = "*".

Once I took stretchCoulumns off all columns, the RatingBar displayed according to the android:numStars value

<RatingBar
android:id="@+id/ratingBar"
style="@style/custom_rating_bar"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:clickable="true"
android:numStars="5"
/>

Configure the number of Stars in the XML file... No need to provide it in Styles or Activity/Fragment .... IMPORTANT: Make sure you Put the WIDTH as Wrap content and weights are not enabled

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.

Another possibility is you are using the Rating in Adapter of list view

View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)

Below params are necessary for all the attributes to work properly:

  1. Set the root to parent
  2. boolean attachToRoot as false

Ex: convertView = inflater.inflate(R.layout.myId, parent, false);

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.

To show a simple star rating in round figure just use this code

public static String getIntToStar(int starCount) {
String fillStar = "\u2605";
String blankStar = "\u2606";
String star = "";


for (int i = 0; i < starCount; i++) {
star = star.concat(" " + fillStar);
}
for (int j = (5 - starCount); j > 0; j--) {
star = star.concat(" " + blankStar);
}
return star;
}

And use it like this

button.setText(getIntToStar(4));

If you are using

android:layout_width="match_parent"

Use wrap_content and it will only show the set numStars :)

android:layout_width="wrap_content"

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

You should just use numStars="5" in your XML, and set android:layout_width="wrap_content".
Then, you can play around with styles and other stuff, but the "wrap_content" in layout_width is what does the trick.

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.

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