2.3.9 RatingBar (Star Rating Bar)
Category Android Basic Tutorial
Introduction to This Section:
>
Wasn't the SeekBar from the last section quite easy? The RatingBar (star rating bar) we're learning about in this section is also very simple. I believe those who have shopped on Taobao are not unfamiliar with this. When you receive a package from the seller, there is often a small piece of paper inside, saying something like "give a five-star review and get a rebate of a certain amount of money". When rating, you can use our star rating bar. Let's take a look at the official documentation first. Official Documentation: RatingBar We can see that this thing has the same class structure as SeekBar, and it is also a subclass of ProgressBar:
That is to say, it also has the relevant properties of ProgressBar. Next, let's explore the unique properties of RatingBar!
1. Basic Usage of RatingBar:
Let's first take a look at what the native SeekBar looks like in 5.0:
—Related Attributes:
android:isIndicator: Whether it is used as an indicator, which users cannot change, default is false android:numStars: How many stars are displayed, must be an integer android:rating: The default rating value, must be a floating point number android:stepSize: The value each rating increases by, must be a floating point number
In addition to the above, there are two styles for us to choose from, but it is not recommended to use them because both of these styles are quite ugly... They are: style="?android:attr/ratingBarStyleSmall" and style="?android:attr/ratingBarStyleIndicator"
—Event Handling: Just set the OnRatingBarChangeListener event for the RatingBar, and then override the onRatingChanged() method!
Implementation Code is as follows:
public class MainActivity extends AppCompatActivity {
private RatingBar 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(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),
Toast.LENGTH_LONG).show();
}
});
}
}
2. Customization:
>
Hehe, we often don't use stars as the standard for rating. Let's change it~ Change the stars to something else, such as smiley faces, with two materials:
Next, just like the previous SeekBar, write a layer-list file:
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 the style.xml, customize the RatingBar Style, add this to style.xml:
<style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
<item name="android:progressDrawable">@drawable/ratingbar_full</item>
<item name="android:minHeight">24dip</item>
<item name="android:maxHeight">24dip</item>
</style>
Finally, set the Ratingbar component in the layout:
<RatingBar
android:id="@+id/rb_normal"
style="@style/roomRatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Running Effect Picture:
Well, the effect is quite good, as for the spacing issue, it is necessary to process the pictures, that is, reserve some space on the left and right when cutting the pictures~!
Summary of This Section:
Okay, that's it for the use of RatingBar, which is quite similar to the previous SeekBar, quite easy~ Yeah, thank you~
-[
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment Example - Bottom Navigation Bar Implementation (Method 1)
5.2.2 Detailed Explanation of Fragment Example - Bottom Navigation Bar Implementation (Method 2)
5.2.3 Detailed Explanation of Fragment Example - Bottom Navigation Bar Implementation (Method 3)
5.2.4 Detailed Explanation of Fragment Example - Bottom Navigation Bar + ViewPager Page Switching
[5.2.5 Detailed Explanation of Fragment Example - Simple Implementation of News (
8.3.4 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 1)
8.3.5 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 2)
8.3.6 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 3)
8.3.7 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 4)
8.3.8 Paint API - Xfermode and PorterDuff Detailed Explanation (Part 5)
8.3.14 Paint API - Several Enum/Constant Values and ShadowLayer Shadow Effects
8.3.17 Canvas API Detailed Explanation (Part 2) - Clipping Method Collection
8.3.18 Canvas API Detailed Explanation (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Encounter
8.4.4 Android Animation Collection - Property Animation - Another Encounter
11.0 "2015 Latest Android Basic Tutorial" Completion Celebration~
12.2 DrySister Viewing Girls App (First Edition) - 2. Parsing Backend Data
[12.3