简体   繁体   中英

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

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

in XML file

                            <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

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 . I can't figure out way this append but you can temporarily solve it by set rounded value before set the float .like this :

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

also don't set setMax just setStepSize . let me know if it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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