The Star rating Bar (Ratingbar) is very similar to the drag bar, and they also have a common parent class absseekbar. In fact, the star rating bar and the use and function of the drag bar are very close: they allow users to drag to change the progress. The biggest difference between Ratingbar and Seekbar is that Ratingbar is going through the stars to show progress.
In order for the program to respond to changes in star rating bar ratings, the program can consider binding a onratingbarchangelistener listener to it.
The star ratings bar supports common XML attributes.
| XML properties |
Description |
| Android:isindicator |
Set whether the star rating bar allows the user to change (true to not allow changes) |
| Android:numstars |
Set the star rating bar total number of stars |
| Android:rating |
Set the star rating bar by default |
| Android:stepsize |
Set the minimum number of stars to change each time |
Example: Changing the transparency of a picture by star rating
Layout/main.xml
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 android:orientation= "vertical"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent">6 7 <ImageView8 Android:id= "@+id/image"9 Android:layout_width= "Fill_parent" Ten Android:layout_height= "240px" One android:src= "@drawable/lijiang" A /> - - <!--define a star rating bar - the <Ratingbar - Android:id= "@+id/rating" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" + Android:numstars= "5" - Android:max= "255" + android:progress= "255" A android:stepsize= "0.5" at /> - - - </LinearLayout>
Main program: Com.example.ratingbar.RatingBarTest.xml
1 PackageCom.example.ratingbar;2 3 Importandroid.support.v7.app.ActionBarActivity;4 ImportAndroid.support.v7.app.ActionBar;5 Importandroid.support.v4.app.Fragment;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.LayoutInflater;8 ImportAndroid.view.Menu;9 ImportAndroid.view.MenuItem;Ten ImportAndroid.view.View; One ImportAndroid.view.ViewGroup; A ImportAndroid.widget.ImageView; - ImportAndroid.widget.RatingBar; - ImportAndroid.widget.RatingBar.OnRatingBarChangeListener; the ImportAndroid.os.Build; - - Public classRatingbartestextendsactionbaractivity { - + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); at - FinalImageView image =(ImageView) Findviewbyid (r.id.image); -Ratingbar Ratingbar =(Ratingbar) Findviewbyid (r.id.rating); - -Ratingbar.setonratingbarchangelistener (NewOnratingbarchangelistener () { - in @Override - Public voidOnratingchanged (Ratingbar Ratingbar,floatrating, to BooleanFromuser) { + //dynamically change the transparency of the image, where 255 is the maximum value of the star rating bar - //5 stars represents the maximum value of 255 theImage.setalpha ((int) (Rating * 255/5)); * } $ });Panax Notoginseng } - the}
Running effect 5 stars:
Running Effect 3 stars: