【问题标题】:Android ratingBar ignoring numStarsAndroid ratingBar 忽略 numStars
【发布时间】:2016-02-24 06:29:38
【问题描述】:

我对 Android 开发非常陌生。我正在尝试将 ratingBar 指示器添加到将成为 ListView 项目的布局屏幕,因此用户可以看到餐厅名称列表及其右侧的星级值。

我想将视图限制为 5 星 - 即它不应显示超过 5 星,并且只有当用户将餐厅评为 5 星时才显示 5 星(在另一个活动中)。

我已经阅读了 thisthisthis,以及关于 ratingBar 的 developer.android.com 文档,但我一辈子都无法让它显示 /em> 5 星。

这是我的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="64dp"
                android:paddingRight="32dp"
                android:paddingTop="4dp"
                android:paddingBottom="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/restaurantNameLabel"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:textColor="#000000"
        android:textSize="16sp"
        tools:text="Shamrock Club"
        />

    <RatingBar
        android:id="@+id/ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:numStars="5"
        android:stepSize=".01"
        android:isIndicator="true"
        android:rating="3"
        android:layout_toEndOf="@id/restaurantNameLabel"/>
</RelativeLayout>

这就是它在 Android Studio 设计视图中的样子;注意有 6+ 星显示。我在这里错过了什么?

【问题讨论】:

    标签: android ratingbar


    【解决方案1】:

    如果您可以从相对布局更改为线性布局,这应该可以正常工作

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="64dp"
                    android:paddingRight="32dp"
                    android:paddingTop="4dp"
                    android:paddingBottom="4dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/restaurantNameLabel"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:textColor="#000000"
            android:textSize="16sp"
            tools:text="Shamrock Club"
            />
    
        <RatingBar
            android:id="@+id/ratingBar"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:numStars="5"
            android:stepSize=".01"
            android:isIndicator="true"
            android:rating="3"
            android:layout_toEndOf="@id/restaurantNameLabel"/>
    
    </LinearLayout>
    

    现在它实际上只会显示 3 颗星,但您可以更改 android:rating 以反映您的喜好。

    【讨论】:

    • 谢谢!那确实奏效了。为什么? RatingBar 是否关心其父级的布局类型?我在文档中没有看到任何表明这一点的内容。
    • 您也可以将其保留为 RelativeLayout,但如果您想将其保留为相关布局,则需要删除 android:layout_alignParentRight="true" 行。在该行和 android:layout_toEndOf="@id/restaurantNameLabel" 行之间,您正在将 RatingBar 拉伸为比您想要的更多的星星。
    • 所以显示的星星数量取决于可用屏幕的宽度,不是通过特定的参数来控制的吗?这似乎有点倒退。
    • 我同意,看起来有一个问题报告了一个与谷歌非常接近的问题,而不久前code.google.com/p/android/issues/… 但它看起来只是因为它是一个老问题而被关闭。您应该向他们报告问题,以便他们有机会时查看。
    猜你喜欢
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多