Android Basics Getting Started tutorial--2.3.9 Ratingbar (star rating bar)
tags (space delimited): Android Basics Getting Started Tutorial
Introduction to this section:
The last section of the Seekbar is not very easy, this section we learn this ratingbar (Star rating bar) is very simple, believe in a treasure,
Bought things on this should not be unfamiliar, received the seller's package, there will be a lot of time there is a small piece of paper, five-star return how many yuan so,
And the rating of the time can be used to our Star rating bar ~ First to look at the official documents
Official Document: Ratingbar
As we can see, this thing is the same as the class structure of Seekbar, and it is also a subclass of ProgressBar:
In other words, he is also useful for ProgressBar related properties, so let's explore Ratingbar's unique properties!
1.RatingBar Basic use:
Let's take a look at what the native seekbar of 5.0 looks like:
--Related attributes:
android:isindicator: is used as an indication that the user cannot change, default false
android:numstars: show how many stars, must be an integer
android:rating: Default rating value, must be a floating-point number
android:stepsize: The value of each increment must be a floating-point number
In addition to these, there are two styles for us to choose, but not recommended, because both of the styles are very ugly ...
They were:
style= "? Android:attr/ratingbarstylesmall"
Style= "? Android:attr/ratingbarstyleindicator"
--Event handling:
Just set the onratingbarchangelistener event for Ratingbar and then rewrite the onratingchanged () Method!
The implementation code is as follows:
Public class mainactivity extends appcompatactivity { PrivateRatingbar Rb_normal;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Rb_normal = (Ratingbar) Findviewbyid (r.id.rb_normal); Rb_normal.setonratingbarchangelistener (NewRatingbar.onratingbarchangelistener () {@Override Public void onratingchanged(Ratingbar Ratingbar,floatRatingBooleanFromuser) {Toast.maketext (mainactivity. This,"Rating:"+ string.valueof (rating), Toast.length_long). Show (); } }); }}
2. Custom Link:
Hey, we will not use the stars as the standard of grading, we have to change Bai ~
Change the star to something else, like a smiley face, two materials:
Next, write a layer-list file like the previous seekbar:
Ratingbar_full.xml:
<?xml version= "1.0" encoding= "Utf-8"?><layer-list xmlns:android="Http://schemas.android.com/apk/res/android"> <item android:id="@android: Id/background"android:drawable=" @mipmap/ic_rating_off1 " /> <item android:id="@android: id/secondaryprogress"android:drawable= "@mipmap/ic_rating_off1" /> <item android:id="@android: id/progress"android:drawable="@ Mipmap/ic_rating_on1 " /> </layer-list>
Then, in Style.xml, customize the Ratingbar Style and add this in style.xml :
<style name = "Roomratingbar" parent= "@android: style/ Widget.ratingbar " > <item name = "android:progressdrawable" > @drawable/ratingbar_full</ item > <item name = > dip</item > <item name = "android:maxheight" > 24 dip</item > </style>
Finally, under the Ratingbar component settings in the layout:
<RatingBar android:id="@+id/rb_normal" style="@style/roomRatingBar" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Run:
OK, the effect can also ha, as to the spacing problem, you need to sit down the picture, it is necessary to cut pictures in the picture around the space to reserve!
This section summarizes:
OK, about the use of ratingbar here, and the front of the Seekbar in fact, very much the same, quite relaxed ~ well, thank you ~
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics Getting Started tutorial--2.3.9 Ratingbar (star rating bar)