How to make a smaller RatingBar?

前端 未结 18 1476
情话喂你
情话喂你 2020-11-27 09:29

I\'ve added a RatingBar in a layout:



        
18条回答
  •  失恋的感觉
    2020-11-27 09:37

    I found an easier solution than I think given by the ones above and easier than rolling your own. I simply created a small rating bar, then added an onTouchListener to it. From there I compute the width of the click and determine the number of stars from that. Having used this several times, the only quirk I've found is that drawing of a small rating bar doesn't always turn out right in a table unless I enclose it in a LinearLayout (looks right in the editor, but not the device). Anyway, in my layout:

                
                    
                
    

    and within my activity:

        final RatingBar minimumRating = (RatingBar)findViewById(R.id.myRatingBar);
        minimumRating.setOnTouchListener(new OnTouchListener()
        { 
            public boolean onTouch(View view, MotionEvent event)
            { 
                float touchPositionX = event.getX();
                float width = minimumRating.getWidth();
                float starsf = (touchPositionX / width) * 5.0f;
                int stars = (int)starsf + 1;
                minimumRating.setRating(stars);
                return true; 
            } 
        });
    

    I hope this helps someone else. Definitely easier than drawing one's own (although I've found that also works, but I just wanted an easy use of stars).

提交回复
热议问题