Android学习笔记之RatingBar

一、简介

RatingBar为评分条控件,默认效果为若干个绿色的星星,如果想将其换成其他自定义图片就要自定义它的style。

RatingBar是SeekBar 和 ProgressBar 的一种扩展,用星星表示等级。

当RatingBar使用默认的大小,用户可以点击/拉拽或使用方向键来设置等级。

它有两种样式(小风格用ratingBarStyleSmall,大风格用ratingBarStyleIndicator),其中大的只适合指示,不适合于用户交互(用户无法改变)。

当使用可以支持用户交互的RatingBar时,无论将控件(widgets)放在它的左边还是右边都是不合适的。

只有当布局的宽被设置为wrap content时,设置的星星数量(通过函数setNumStars(int)或者在XML的布局文件中定义)将显示出来(如果设置为另一种布局宽的话,后果无法预知)。

次级进度一般不应该被修改,因为他仅仅是被当作星型部分内部的填充背景。

二、属性

属性名称 描述
android:isIndicator RatingBar是否是一个指示器(用户无法进行更改)
android:numStars 显示的星型数量,必须是一个整形值,像“100”。
android:rating 默认的评分,必须是浮点类型,像“1.2”。
android:stepSize 评分的步长,必须是浮点类型,像“1.2”。

三、方法:

名称 描述
getNumStars() 返回显示星星总共的的数目。
getRating() 获取当前等级(满星星的数目)。
getStepSize() 得到RatingBar的步长。
isIndicator() 返回这RatingBar是否只是一个指示器(只是起指示作用,用户无法进行交互)。
setMax(int max) 设置这个进度条的范围为0……max
setNumStars(int numStars) 设置星星的数目。
setRating(float rating) 设置等级(星星的填充数目)。
setStepSize(float stepSize) 设置RatingBar的步长。

四、范例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package xiaosi.ratingbar;  

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

public class RatingBarActivity extends Activity {
/** Called when the activity is first created. */
private RatingBar ratingbar = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ratingbar = (RatingBar)findViewById(R.id.ratingbar);
ratingbar.setOnRatingBarChangeListener(new RatingBarListener());
}

private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{

public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
System.out.println("等级:" + rating);
System.out.println("星星:" + ratingBar.getNumStars());
}
}
}

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一种风格(可交互)" />
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二种风格(ratingBarStyleSmall)" />
<RatingBar
android:id="@+id/ratingbar2"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三种风格(ratingBarStyleIndicator)" />
<RatingBar
android:id="@+id/ratingbar3"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"/>
</LinearLayout>

参考:http://blog.csdn.net/sjf0115/article/details/7249777