[英]Android Rating Bar unable to set decimal values
I am trying to set the rating bar's inital rating to a float value. 我试图将等级栏的初始等级设置为浮动值。 But the output is always completely filled stars! 但是输出总是完全充满星星! I have no idea why this is happening. 我不知道为什么会这样。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ratingText = (TextView) findViewById(R.id.rating);
ratingBar = ((RatingBar) findViewById(R.id.ratingBar1));
ratingBar.setRating(Float.parseFloat("3.5"));
((RatingBar) findViewById(R.id.ratingBar1))
.setOnRatingBarChangeListener(this);
}
This is the rating bar: 这是评分栏:
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:stepSize="0.5"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp" />
You are setting the stepSize="0.5". 您正在设置stepSize =“ 0.5”。 You should change it to 0.1. 您应该将其更改为0.1。
Try to do it by programattically like this 尝试通过编程方式做到这一点
ratingBar = ((RatingBar) findViewById(R.id.ratingBar1));
ratingBar.setStepSize(0.5f);
ratingBar.ratingBar.setNumStars(5);
ratingBar.ratingBar.setMax(5);
ratingBar.setRating(Float.parseFloat("3.5"));
You just remove the toolbar in your project. 您只需删除项目中的工具栏。 I think there is some issue in toolbar. 我认为工具栏存在一些问题。 I tried as follows, it works 我尝试如下,它的工作原理
ratingBar = ((RatingBar) findViewById(R.id.ratingBar1));
ratingBar.setRating(Float.parseFloat("3.6"));
ratingBar = ((RatingBar) findViewById(R.id.ratingBar1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.