Ratingbar Custom star rating style for Android controls _android

Source: Internet
Author: User

First, Ratingbar simple introduction

Ratingbar is based on an extension of the Seekbar (drag bar) and ProgressBar (status bar) to display ratings using a star, and when using the default Ratingbar, the user can set the score by touching/dragging/pressing (such as a remote control). Ratingbar with two models, a small style ratingbarstylesmall, large style for ratingbarstyleindicator, large only suitable for instructions, do not apply to user interaction.

The effect chart shows:

Second, the example

1. layout file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:paddingleft=" 10dip "android:layout_width=" Match_parent "Android:
layout_height= "Match_parent" > <ratingbar android:id= "@+id/ratingbar1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:numstars= "3" android:rating= "2.5"/> <ratingbar android:id= "@+id /ratingbar2 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:numStars=" 5 "Android
: rating= "2.25"/> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"
android:layout_margintop= "10dip" > <textview android:id= "@+id/rating" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <ratingbar android:id= "@+id/small_ratingbar"? style= Ratingbarstylesmall "android:layout_marginleft=" 5dip "android:layout_width=" Wrap_content "Android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical"/> </LinearLayout> <ratingbar Android:id= "@+id/indicator_ratingbar" "style="? Android:attr/ratingbarstyleindicator "android:layout_marginLeft=" 5dip "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_gravity=" Center_ Vertical "/> </LinearLayout>

2.Java Code

Package WJQ.
Widgetdemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.RatingBar;
Import Android.widget.TextView;
Import Android.widget.RatingBar.OnRatingBarChangeListener; public class Ratingbardemo extends activity implements Onratingbarchangelistener {private Ratingbar msmallratingbar; pri
Vate Ratingbar Mindicatorratingbar;
Private TextView Mratingtext; * * * (non-javadoc) * * @see android.app.activity#oncreate (android.os.Bundle)/@Override protected void OnCreate (Bundle Savedinstancestate) {//TODO auto-generated Method Stub super.oncreate (savedinstancestate); Setcontentview (
R.layout.ratingbarpage);
Mratingtext = (TextView) Findviewbyid (r.id.rating); We Copy the most recently changed rating on to these indicator-only//rating Bars Mindicatorratingbar = (Ratingbar) fi
Ndviewbyid (R.id.indicator_ratingbar);
Msmallratingbar = (Ratingbar) Findviewbyid (R.id.small_ratingbar); The different rating bars in the layout.
Assign the listener to us. (RatIngbar) Findviewbyid (R.ID.RATINGBAR1)). Setonratingbarchangelistener (this);
((Ratingbar) Findviewbyid (R.ID.RATINGBAR2)). Setonratingbarchangelistener (this); @Override public void onratingchanged (Ratingbar ratingbar, float rating, Boolean fromuser) {final int numstars = rating
Bar.getnumstars ();
Mratingtext.settext ("popular degree" + rating + "/" + Numstars); 
Since This rating bar are updated to reflect any of the other rating//bars, we are should update it to the current values.
if (Mindicatorratingbar.getnumstars ()!= numstars) {mindicatorratingbar.setnumstars (numstars);
Msmallratingbar.setnumstars (Numstars); } if (Mindicatorratingbar.getrating ()!= rating) {mindicatorratingbar.setrating (rating); Msmallratingbar.setrating (
Rating);
Final float ratingbarstepsize = Ratingbar.getstepsize ();
if (Mindicatorratingbar.getstepsize ()!= ratingbarstepsize) {mindicatorratingbar.setstepsize (ratingBarStepSize);
Msmallratingbar.setstepsize (ratingbarstepsize); }
}
}

About Android controls Ratingbar custom star rating style I would like to introduce you here, I hope to help you, this article is not good to write also please heroes more advice!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.