<1> Introduction
Ratingbar is a scoring bar control. By default, it has several green stars. If you want to change it to another custom image, you need to customize its style.
Ratingbar is an extension of seekbar and progressbar, which uses stars to represent the level.
When ratingbar uses the default size, you can click/drag or use the direction keys to set the level. The default size is used when ratingbar is used.
It has two styles (ratingbarstylesmall for small styles and ratingbarstyleindicator for large styles). The larger one is only suitable for instructions and is not suitable for user interaction (users cannot change ).
When you use a ratingbar that supports user interaction, it is not appropriate to place the control (widgets) on the left or right.
Only when the layout width is set to wrap content, the number of stars is set (defined by the setnumstars (INT) function or in the XML layout file) it will be displayed (if it is set to another layout width, the consequences will be unpredictable ).
Generally, the secondary progress should not be modified because it is only used as the filling background of the star part.
<2> attributes
Attribute name |
Description |
Android: isindicator |
Whether the ratingbar is an indicator (the user cannot change it) |
Android: numstars |
The number of displayed stars, which must be an integer value, such as "100 ". |
Android: Rating |
The default score must be a floating point type, such as "1.2 ". |
Android: stepsize |
The scoring step size must be of the floating point type, such as "1.2 ". |
<3> method:
Public int getnumstars ()
Returns the total number of stars displayed.
Public float getrating ()
Obtain the current level (number of stars ).
Public float
Getstepsize ()
Get the step of ratingbar.
Public Boolean
Isindicator ()
Returns whether the ratingbar is only an indicator (it only serves as an indicator and users cannot interact with each other ).
Public synchronized void
Setmax (INT max)
Set the progress bar to 0 ...... Max
Public void
Setnumstars (INT numstars)
Set the number of stars.
Public void
Setrating (float rating)
Set the level (number of stars filled ).
Public void
Setstepsize (float stepsize)
Set the step size of the ratingbar.
<4> example
Package xiaosi. ratingbar; import android. app. activity; import android. OS. bundle; import android. widget. ratingbar; public class ratingbaractivity extends activity {/** called when the activity is first created. */private ratingbar = NULL; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); ratingbar = (ratingbar) findviewbyid (R. id. ratingbar); ratingbar. setonratingbarchangelistener (New ratingbarlistener ();} private class ratingbarlistener implements ratingbar. onratingbarchangelistener {public void onratingchanged (ratingbar, float rating, Boolean fromuser) {system. out. println ("grade:" + rating); system. out. println ("Star:" + ratingbar. getnumstars ());}}}
Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "first style (interactive)"/> <ratingbar Android: id = "@ + ID/ratingbar" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Ndroid: numstars = "5" Android: stepsize = "1"/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "ratingbarstylesmall"/> <ratingbar Android: Id = "@ + ID/ratingbar2" style = "? Android: ATTR/ratingbarstylesmall "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: numstars =" 5 "Android: stepsize =" 1 "/> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "third style (ratingbarstyleindicator)"/> <ratingbar Android: id = "@ + ID/ratingbar3" style = "? Android: ATTR/ratingbarstyleindicator "Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: numstars =" 5 "Android: stepsize =" 1 "/> </linearlayout>