繁体   English   中英

如何让 RatingBar 显示五颗星

[英]How to make RatingBar to show five stars

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

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

提前致谢,罗兰

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

在代码中

mRatingBar.setRating(int)

RatingBar.setNumStar文档说:

设置要显示的星数。 为了正确显示这些内容,建议此小部件的布局宽度为环绕内容。

所以将布局宽度设置为wrap_content应该可以解决这个问题。

这对我RatingBarRatingBar应该在LinearLayout而不是将布局宽度设置为包装RatingBar内容。

此外,如果您设置layout_weight ,这将取代numStars属性。

您应该只在 XML 中使用numStars="5"并设置android:layout_width="wrap_content"
然后,您可以尝试使用样式和其他东西,但是 layout_width 中的“wrap_content”才是诀窍。

如果您正在使用

android:layout_width="match_parent"

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

配置 XML 文件中的星星数...无需在样式或活动/片段中提供它.... 重要提示:确保将 WIDTH 作为 Wrap 内容和权重未启用

我还面临着超过星星数而不是指定星星数的问题。 为此,我们不想担心布局类型是相对布局还是线性布局。 简单地使用宽度如下:

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


避免match_parent 和 fill_parent用于ratingbar
希望这些东西会有所帮助。

即使我遇到了@Roland 的问题,我也包含了一个名为

android:layout_alignParentRight="true" 

在 XML 格式的RatingBar声明中。 此属性阻止设置所需的星星和设置NumStars

随时发布您遇到的问题!

对我来说,在删除填充之前我遇到了一个问题。 看起来某些设备上存在一个错误,它不会改变视图的大小以适应填充,而是压缩内容。

删除padding ,改用layout_margin

要在圆形图中显示简单的星级,只需使用此代码

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

并像这样使用它

button.setText(getIntToStar(4));

您可以在 xml 布局旁边添加五颗星的默认评级

android:rating="5"

编辑:

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

如果您将RatingBar包装在ConstraintLayout ,其宽度为match_constraint ,则编辑器预览将显示与其实际宽度成正比的星数,无论您是否设置android:numStars属性。 使用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>

我发现RatingBar拉伸到最大数量的星星,因为它被包含在一个带有android:stretchColumns = "*"属性的表中。

一旦我从所有列中取出了stretchCoulumnsRatingBar根据android:numStars值显示

另一种可能性是您在列表视图的适配器中使用评级

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

以下参数是所有属性正常工作所必需的:

  1. root设置parent
  2. boolean attachToRootfalse

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

默认值在 xml 布局中使用andoid:rating设置。

这里的实际情况是使用wrap_content而不是match_parent 如果您将使用match_parent布局将填充星星,即使它多于numStars变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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