欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android控件之RatingBar自定义星级评分样式

程序员文章站 2024-02-22 19:14:22
一、ratingbar简单介绍 ratingbar是基于seekbar(拖动条)和progressbar(状态条)的扩展,用星形来显示等级评定,在使用默认ratingba...

一、ratingbar简单介绍

ratingbar是基于seekbar(拖动条)和progressbar(状态条)的扩展,用星形来显示等级评定,在使用默认ratingbar时,用户可以通过触摸/拖动/按键(比如遥控器)来设置评分, ratingbar自带有两种模式 ,一个小风格 ratingbarstylesmall,大风格为ratingbarstyleindicator,大的只适合做指示,不适用与用户交互。

效果图展示:

Android控件之RatingBar自定义星级评分样式

二、实例

1.布局文件

<?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="?android:attr/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代码

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;
private 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) findviewbyid(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 = ratingbar.getnumstars();
mratingtext.settext( 
" 受欢迎度" + rating + "/" + numstars);
// since this rating bar is updated to reflect any of the other rating
// bars, we 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);
}
}
}

关于android控件之ratingbar自定义星级评分样式就给大家介绍这里,希望对大家有所帮助,本文写的不好还请各位大侠多多指教!