Android 评分栏只显示满星,不显示半星
Android Rating Bar shows only full stars, not half stars too
我在 Android 应用程序中使用评级栏。但是它只显示满星,而我也想要半星。
我插入了一个带有名称和 ratingValue 的 SQLite 行,其中 ratingValue 为“3.5”,但我的应用程序将其变为“4”。即使我的StepSize是“0.5”,连默认值都改成了“3”
编辑:
如果您单击列表行,它会弹出另一个评级栏,您可以 "rate" 如果您单击一个按钮,它会将评级值发送到数据库中。列表项 RatingBar 仅用于显示 "average" 评级,但这是错误的地方,它将值四舍五入
编辑 2:
将步长设置为 0.1,现在奇怪的是,只有中间的星接受浮点数......
剩下的星星还是只有满
如果我将评级设置为 1.2,只有第二颗星接受浮点值,0.2 第一颗,等等,但其他星星(不在该范围内)只有或满或空
这是一些代码
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());
}
可重复使用class
public float getRating() { return Rating; }
SQLite Table 评分列
COLUMN_RATING + " real not null);"
最后,从数据库获取
c.getFloat(COLUMN_RATING_COL));
一些图片来解释
首先,评级小部件的定义方式无法接受值。 Clickable 设置为 false,isIndicator 设置为 true。要接受输入,请按以下方式定义小部件
<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" />
对于不确定如何设置变量 Rating 的值
查看图片以了解小部件的外观 like!
我认为您不需要为此使用任何库,我只是尝试了一下,它可以正常工作。
<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"/>
我将 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);
}
});
我在 Android 应用程序中使用评级栏。但是它只显示满星,而我也想要半星。
我插入了一个带有名称和 ratingValue 的 SQLite 行,其中 ratingValue 为“3.5”,但我的应用程序将其变为“4”。即使我的StepSize是“0.5”,连默认值都改成了“3”
编辑: 如果您单击列表行,它会弹出另一个评级栏,您可以 "rate" 如果您单击一个按钮,它会将评级值发送到数据库中。列表项 RatingBar 仅用于显示 "average" 评级,但这是错误的地方,它将值四舍五入
编辑 2: 将步长设置为 0.1,现在奇怪的是,只有中间的星接受浮点数...... 剩下的星星还是只有满 如果我将评级设置为 1.2,只有第二颗星接受浮点值,0.2 第一颗,等等,但其他星星(不在该范围内)只有或满或空
这是一些代码
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());
}
可重复使用class
public float getRating() { return Rating; }
SQLite Table 评分列
COLUMN_RATING + " real not null);"
最后,从数据库获取
c.getFloat(COLUMN_RATING_COL));
一些图片来解释
首先,评级小部件的定义方式无法接受值。 Clickable 设置为 false,isIndicator 设置为 true。要接受输入,请按以下方式定义小部件
<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" />
对于不确定如何设置变量 Rating 的值
查看图片以了解小部件的外观 like!
我认为您不需要为此使用任何库,我只是尝试了一下,它可以正常工作。
<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"/>
我将 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);
}
});