[英]How to make RatingBar to show five stars
I am following the standard example of how to add a RatingBar
.我正在遵循如何添加RatingBar
的标准示例。 To control the number of stars I tried to use android:numStars="5"
.为了控制星星的数量,我尝试使用android:numStars="5"
。 The problem is that the number of stars doesn't seem to do anything at all.问题是星星的数量似乎根本没有任何作用。 In portrait-layout I get 6 stars and when I flip the phone I get about 10 stars.在纵向布局中,我得到 6 颗星,当我翻转手机时,我得到大约 10 颗星。 I tried to set the number of stars in my Activity ( myBar.setNumStars(5)
) that loads the xml but there was no success with that option either.我尝试在加载 xml 的 Activity ( myBar.setNumStars(5)
) 中设置星数,但该选项也没有成功。
So my question is how do I define my layout so that it will only show five stars?所以我的问题是如何定义我的布局,使其只显示五颗星? Set numStars
doesn't seem to work.设置numStars
似乎不起作用。
Thanks in advance, Roland提前致谢,罗兰
<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 RatingBar.setNumStar
documentation says: RatingBar.setNumStar
文档说:
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.所以将布局宽度设置为wrap_content
应该可以解决这个问题。
这对我RatingBar
: RatingBar
应该在LinearLayout
而不是将布局宽度设置为包装RatingBar
内容。
此外,如果您设置layout_weight
,这将取代numStars
属性。
You should just use numStars="5"
in your XML, and set android:layout_width="wrap_content"
.您应该只在 XML 中使用numStars="5"
并设置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.然后,您可以尝试使用样式和其他东西,但是 layout_width 中的“wrap_content”才是诀窍。
If you are using如果您正在使用
android:layout_width="match_parent"
Use wrap_content and it will only show the set numStars :)使用 wrap_content 它将只显示设置的 numStars :)
android:layout_width="wrap_content"
<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配置 XML 文件中的星星数...无需在样式或活动/片段中提供它.... 重要提示:确保将 WIDTH 作为 Wrap 内容和权重未启用
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 .请避免将match_parent 和 fill_parent用于ratingbar 。
Hope the things would be helpful.希望这些东西会有所帮助。
Even i was facing the issue @Roland , I had included one more attribute called即使我遇到了@Roland 的问题,我也包含了一个名为
android:layout_alignParentRight="true"
in my RatingBar
declaration in XML.在 XML 格式的RatingBar
声明中。 This attribute prevented from setting of the stars required and setting up the NumStars
此属性阻止设置所需的星星和设置NumStars
Keep posted on the issues you come across !随时发布您遇到的问题!
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.删除padding
,改用layout_margin
。
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)); button.setText(getIntToStar(4));
You can add default rating of five stars in side the xml layout您可以在 xml 布局旁边添加五颗星的默认评级
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" />
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.如果您将RatingBar
包装在ConstraintLayout
,其宽度为match_constraint
,则编辑器预览将显示与其实际宽度成正比的星数,无论您是否设置android:numStars
属性。 Use wrap_content
to get the correct preview:使用wrap_content
获得正确的预览:
<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" />
<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 = "*"
.我发现RatingBar
拉伸到最大数量的星星,因为它被包含在一个带有android:stretchColumns = "*"
属性的表中。
Once I took stretchCoulumns
off all columns, the RatingBar
displayed according to the android:numStars
value一旦我从所有列中取出了stretchCoulumns
, RatingBar
根据android:numStars
值显示
View android.view.LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)
Below params are necessary for all the attributes to work properly:以下参数是所有属性正常工作所必需的:
root
to parent
将root
设置parent
attachToRoot
as false
boolean attachToRoot
为false
Ex: convertView = inflater.inflate(R.layout.myId, parent, false);例如: convertView = inflater.inflate(R.layout.myId, parent, false);
默认值在 xml 布局中使用andoid:rating设置。
The actual thing over here is to use wrap_content
instead of match_parent
.这里的实际情况是使用wrap_content
而不是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.如果您将使用match_parent
布局将填充星星,即使它多于numStars变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.