【发布时间】: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>
【问题讨论】:
-
很遗憾,我不得不寻找第三个评级栏库才能使其正常工作......