简体   繁体   English

Android评分栏仅显示全星,也不显示半星

[英]Android Rating Bar shows only full stars, not half stars too

I'm using rating bars in my Android application. 我在Android应用程序中使用评分栏。 But it only shows full stars, when I also want half stars. 但是,当我也想要半颗星时,它只显示满星。

I inserted a SQLite row with a name and the ratingValue, in there the ratingValue stands as "3.5" but my application turns it into "4". 我插入了一个带有名称和ratingValue的SQLite行,在该行中ratingValue表示为“ 3.5”,但我的应用程序将其变为“ 4”。 Even if my StepSize is "0.5", even the default value is changed to "3" 即使我的StepSize为“ 0.5”,默认值也更改为“ 3”

EDIT: If you click on the listrow, it dialogs another ratingbar which you can "rate" and if you click on a button, it sends the rating value into the database. 编辑:如果您单击列表行,它会对话另一个评级栏,您可以对其进行“评级”,如果单击按钮,它将评级值发送到数据库中。 The listitem RatingBar is only for showing the "average" rating but that's where it goes wrong, it rounds the value up 列表项RatingBar仅用于显示“平均”评级,但那是错误的地方,它会将值四舍五入

EDIT 2: set the stepsize to 0.1 and now, strangely, only the middle star accepts the float number... The rest of the stars are still only full And if i set rating on 1.2, only the second star accepts float value, 0.2 the first, etc but the other stars (not in that range) are only or full or empty 编辑2:将步长设置为0.1,现在,奇怪的是,只有中星才接受浮点数...其余的星星仍然只有满星如果我将等级设置为1.2,则只有第二颗星接受浮点值0.2第一颗,以此类推,但其他星星(不在该范围内)只有或满或为空

Here's some code 这是一些代码

ListItem_layout ListItem_layout

<TableRow>    
    <RatingBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/sportsRatingBar"
        android:gravity="center_horizontal"
        android:focusableInTouchMode="false"
        android:isIndicator="true"
        android:clickable="false"
        android:focusable="false"
        android:layout_column="1"
        android:rating="1.1"
        android:stepSize="0.1" />

ListItem 项目清单

if(ratingBar!=null) {
                ratingBar.setRating(p.getRating());
                ratingBar.setTag(p.getId());
            }

Reusable class 可重用类

public float getRating() { return Rating; }

SQLite Table Column for rating 用于评级的SQLite表列

 COLUMN_RATING + " real not null);"

And last, getting from database 最后,从数据库获取

c.getFloat(COLUMN_RATING_COL));

some images to explain 一些图片来说明 在将第一个等级栏设置为2.6之后

I think you don't need any library for this, I just tried it, and it working. 我认为您不需要任何库,我只是尝试了一下,它就可以了。

<android.support.v7.widget.AppCompatRatingBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/ratingBarStyle"
    android:background="@android:color/holo_red_dark"
    android:id="@+id/ratingBar1"
    android:stepSize="0.1"
    android:rating="2.5"/>

I set value of start to 4.5 and it is working that way. 我将start的值设置为4.5,并且它以这种方式工作。

RatingBar ratingBar = (RatingBar) findViewById(R.id.coloredRatingBar1);
    ratingBar.setRating(4.8f);
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            Log.i(TAG, "onRatingChanged: rating : "+rating);
        }
    });

First of all the the way rating widget is defined it cannot accept values. 首先,定义评级小部件的方式不能接受值。 Clickable is set to false and isIndicator is made true. Clickable设置为false,isIndicator设置为true。 To accept input define the widget in the following way 要接受输入,请按以下方式定义小部件

<RatingBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/sportsRatingBar"
        android:gravity="center_horizontal"
        android:rating="2.5"
        android:stepSize="0.5" />

For the getting the values not sure about how the variable Rating is being set 为了获取值,不确定如何设置变量Rating

Check out the image for how the widget looks like ! 检查出的小部件看起来多么像喜欢

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

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