简体   繁体   English

android评级栏仅显示int值

[英]android rating bar shows only int values

I use this code to show a decimal value by a rating bar. 我使用此代码通过等级栏显示十进制值。 but it round the value and show first bigger int 但它舍入值并显示第一个更大的int

    ratingBar.setNumStars(5);
    ratingBar.setMax(5);
    ratingBar.setStepSize(0.5f);
    ratingBar.setRating(Float.parseFloat("2.5"));

in XML file 在XML文件中

                            <RatingBar
                            android:id="@+id/ratingBar1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:max="5"
                            android:layoutDirection="ltr"
                            android:numStars="5"
                            android:stepSize="0.5" />

the image I see in android 我在android中看到的图像

Layout: 布局:

    <RatingBar android:id="@+id/ratingbar"  android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:numStars="5" 
android:stepSize="0.5"/>

Activity: 活动:

private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ratingBar = (RatingBar)findViewById(R.id.ratingBar);
    assert ratingBar != null;

    dosomething();
    ratingBar.setRating(0.0f);
}

private void dosomething() {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            ratingBar.setRating(2.5f);
        }
    }, 2000);
}

Result in a form Image: 生成表单图像: 在此处输入图片说明

I test your code in sdk 23 and have same problem . 我在sdk 23测试了您的代码,并sdk 23同样的问题。 I can't figure out way this append but you can temporarily solve it by set rounded value before set the float .like this : 我无法弄清楚这种追加的方式,但是您可以在设置float之前通过设置舍入值来临时解决它,如下所示:

  ratingBar.setRating(Math.round( 2.5f));
  ratingBar.setRating( 2.5f);

also don't set setMax just setStepSize . 也不要设置setMax只是setStepSize let me know if it worked. 让我知道是否有效。

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

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