【问题标题】:Rating bar shows half star despite setting step to 1尽管将步长设置为 1,但评分栏显示半星
【发布时间】:2019-03-08 11:37:51
【问题描述】:

我正在向自定义组件动态添加评分栏。我从服务器接收评级,最初显示正常。但是,例如,如果我收到 5 星评级,如果我单击第二颗星,则将 3 星、4 星和 5 星绘制为各获得半星,这对我来说毫无意义。如果我从服务器收到 0 颗星,修改工作正常,并且星星被完美绘制,如果我从服务器收到 2 颗星,星星 3 4 和 5 工作正常,但是星星 1 和 2 被绘制就像收到半颗星一样。 .

评分栏的XML是这样的:

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingbar"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/eventsubtitle"
    android:stepSize="1.0"
    android:max="5"/>

以及 RatingBar 的代码:

ratingBar.setNumStars(5);
            if(!agenda.getRating().equals("null") && !agenda.getRating().equals("") && agenda.getRating()!=null){
                ratingBar.setRating(Float.parseFloat(agenda.getRating()));
            }else{
                ratingBar.setRating(0F);
            }

            ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                    Log.i("David", "sending rating: "+rating);//This shows an x.0 value always
                    float ratingToSet=(float) Math.ceil(rating);
                    Log.i("David", "rating: "+ratingToSet);//This shows an x.0 value always
                    ratingBar.setRating(ratingToSet);
                    //rating sent to server
            });

有人可以帮助我吗?谢谢。

编辑:截图:

编辑 2:布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timein"
    android:textSize="30sp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_marginStart="10dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/timeout"
    android:layout_alignStart="@+id/timein"
    android:layout_below="@+id/timein"
    android:textSize="30sp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/eventtitle"
    android:layout_toEndOf="@+id/timein"
    android:layout_marginStart="40dp"
    android:textStyle="bold"
    android:textSize="18sp"
    android:layout_marginTop="20dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/eventsubtitle"
    android:layout_alignStart="@+id/eventtitle"
    android:layout_below="@+id/eventtitle"
    android:textSize="15sp"/>

<RatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/ratingbar"
    android:layout_alignParentEnd="true"
    android:layout_below="@+id/eventsubtitle"
    android:stepSize="1.0"/>



<ImageView
    android:layout_width="2dp"
    android:layout_height="wrap_content"
    android:id="@+id/verticalline"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dp"
    android:layout_toEndOf="@+id/timein"
    android:layout_marginStart="20dp"
    android:layout_alignBottom="@+id/speakerrl"
    android:background="@color/colorPrimary"
    android:layout_marginBottom="50dp"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/speakerrl"
    android:layout_below="@+id/timeout"
    android:layout_marginTop="30dp">

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/imagen"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="20dp"/>

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@android:drawable/ic_menu_directions"
        android:id="@+id/arrow"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/imagen"
        android:layout_marginStart="60dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/speaker"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/arrow"
        android:layout_marginStart="10dp"
        android:textSize="18sp"
        android:textStyle="bold"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/speakersubtitle"
        android:layout_alignStart="@+id/speaker"
        android:layout_below="@+id/speaker"
        android:textSize="15sp"/>

</RelativeLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/askbutton"
    android:text="@string/askquestion"
    android:layout_below="@+id/speakerrl"
    android:layout_centerHorizontal="true"
    android:background="@drawable/buttonshape"
    android:layout_marginBottom="20dp"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_below="@+id/askbutton"
    android:background="@color/colorPrimary"/>
</RelativeLayout>

【问题讨论】:

  • 很遗憾,我不得不寻找第三个评级栏库才能使其正常工作......

标签: android ratingbar


【解决方案1】:

以编程方式添加最高评分,它应该可以解决问题

ratingBar.setMax(5);

编辑:

用并检查替换您的评分栏

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/eventsubtitle">

    <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:id="@+id/ratingbar"
            android:stepSize="1.0"/>

</LinearLayout>

【讨论】:

  • 没有区别,如果我从服务器收到5星评级,然后修改为2星,例如3星4和5星看起来每个星都获得半星,如截图所示。跨度>
  • 你能添加完整的布局吗?
  • 我正在将完整的布局添加到问题中。它是添加到另一个布局内的 LinearLayout 的布局,该布局被添加到 ViewFlipper...
  • ok 为评级栏添加另一个父线性布局,除了评级栏并检查之外别无其他内容
  • 不走运,行为是一样的。
【解决方案2】:

ratingBar.setStepSize(1f);为我工作

【讨论】:

    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 2019-01-13
    • 2021-02-06
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多