android 评分条 RatingBar 使用及自定义

一、先上效果图片:

   第一个是自定义;

   第二个是原生的:

 

二、atingBar 介绍:

  RatingBar是基于SeekBarProgressBar的扩展,用星型来显示等级评定。使用RatingBar的默认大小时,用户可以触摸/拖动或使用键来设置评分,它有两种样式(小风格用ratingBarStyleSmall,大风格用ratingBarStyleIndicator),其中大的只适合指示,不适合于用户交互。

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

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

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

三、嵌套类

  接口:RatingBar.OnRatingBarChangeListener

  一个回调函数,当星级进度改变时修改客户端的星级。

四、XML属性

属性名称

描述

Android:isIndicator

RatingBar是否是一个指示器(用户无法进行更改)

android:numStars

显示的星型数量必须是一个整形值,像“100”。

android:rating

默认的评分,必须是浮点类型,像“1.2”。

android:stepSize

评分的步长必须是浮点类型,像“1.2”。

 

  五、公共方法

 

public int getNumStars ()

    返回显示的星型数量

      返回值

                  显示的星型数量

 

  public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()

返回值

监听器(可能为空)监听评分改变事件

 

  public float getRating ()

  获取当前的评分(填充的星型的数量)

  返回值

  当前的评分

 

  public float getStepSize ()

  获取评分条的步长

  返回值

  步长

 

  public boolean isIndicator ()

返回值

                判断当前的评分条是否仅仅是一个指示器(注:即能否被修改)

 

  public void setIsIndicator (boolean isIndicator)

  设置当前的评分条是否仅仅是一个指示器(这样用户就不能进行修改操作了)

  参数

  isIndicator       Bool值,是否是一个指示器

 

  public synchronized void setMax (int max)

  设置评分等级的范围,从0max

  参数

  max         评分条最大范围。

 

  public void setNumStars (int numStars)

  设置显示的星型的数量。为了能够正常显示它们,建议将当前widget的布局宽度设置为

wrap content

  参数

  numStars         星型的数量

 

  public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

  设置当评分等级发生改变时回调的监听器

  参数

  listener  监听器

 

  public void setRating (float rating)

  设置分数(星型的数量)

  参数

  rating      设置的分数

 

  public void setStepSize (float stepSize)

  设置当前评分条的步长(step size

  参数

  stepSize 评分条的步进。例如:如果想要半个星星,它的值为0.5

四、XML属性

属性名称

描述

Android:isIndicator

RatingBar是否是一个指示器(用户无法进行更改)

android:numStars

显示的星型数量必须是一个整形值,像“100”。

android:rating

默认的评分,必须是浮点类型,像“1.2”。

android:stepSize

评分的步长必须是浮点类型,像“1.2”。

 

  五、公共方法

 

public int getNumStars ()

    返回显示的星型数量

      返回值

                  显示的星型数量

 

  public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()

返回值

监听器(可能为空)监听评分改变事件

 

  public float getRating ()

  获取当前的评分(填充的星型的数量)

  返回值

  当前的评分

 

  public float getStepSize ()

  获取评分条的步长

  返回值

  步长

 

  public boolean isIndicator ()

返回值

                判断当前的评分条是否仅仅是一个指示器(注:即能否被修改)

 

  public void setIsIndicator (boolean isIndicator)

  设置当前的评分条是否仅仅是一个指示器(这样用户就不能进行修改操作了)

  参数

  isIndicator       Bool值,是否是一个指示器

 

  public synchronized void setMax (int max)

  设置评分等级的范围,从0max

  参数

  max         评分条最大范围。

 

  public void setNumStars (int numStars)

  设置显示的星型的数量。为了能够正常显示它们,建议将当前widget的布局宽度设置为

wrap content

  参数

  numStars         星型的数量

 

  public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

  设置当评分等级发生改变时回调的监听器

  参数

  listener  监听器

 

  public void setRating (float rating)

  设置分数(星型的数量)

  参数

  rating      设置的分数

 

  public void setStepSize (float stepSize)

  设置当前评分条的步长(step size

  参数

  stepSize 评分条的步进。例如:如果想要半个星星,它的值为0.5

六、android评分条RatingBar自定义设置

sdk23 RatingBar为评分条控件,默认效果为若干个灰色的星星,如果想将其换成其他自定义图片就要自定义它的style。首先是布局文件:

    <RatingBar
        android:id="@+id/ratingone"
        android:numStars="5"  设置星的个数,注意下边的宽度要设置为wrap_content
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rating="3"   设置默认选中星的个数
        android:stepSize="0.5"  设置评分的间隔,每次增加或者减少多少
        android:isIndicator="false"/> 设置 不允许用户进行评分操作,flase 表示允许

 其中android:numStars="5"设置显示的星星数量为5; android:rating="5"设置选中的数量为5,也就是全部选中

android:isIndicator="true"设置评分条只显示结果无法通过点击改变选中状态。

然后在res/values目录下建立styles.xml文件 代码:

<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'宋体';font-size:12.0pt;"><span style="color:#e8bf6a;"><style </span><span style="color:#bababa;">name=</span><span style="color:#6a8759;">"myRatingBarStyle" </span><span style="color:#bababa;">parent=</span><span style="color:#6a8759;">"@android:style/Widget.RatingBar"</span><span style="color:#e8bf6a;">>
</span><span style="color:#e8bf6a;">    <item </span><span style="color:#bababa;">name=</span><span style="color:#6a8759;">"android:progressDrawable"</span><span style="color:#e8bf6a;">></span>@drawable/my_rating_bar<span style="color:#e8bf6a;"></item>
</span><span style="color:#e8bf6a;">    <item </span><span style="color:#bababa;">name=</span><span style="color:#6a8759;">"android:minHeight"</span><span style="color:#e8bf6a;">></span>16dp<span style="color:#e8bf6a;"></item>
</span><span style="color:#e8bf6a;">    <item </span><span style="color:#bababa;">name=</span><span style="color:#6a8759;">"android:maxHeight"</span><span style="color:#e8bf6a;">></span>16dp<span style="color:#e8bf6a;"></item>
</span><span style="color:#e8bf6a;"></style></span>

android:progressDrawable为评分条图案。接下来在res/drawable目录下建立my_rating_bar.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="@drawable/star1"></item>
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/star1"></item>
    <item android:id="@android:id/progress"
        android:drawable="@drawable/star2"></item>
</layer-list>

 <item android:id="@+android:id/progress" android:drawable="@drawable/rating_show" />为设置评分图案为  rating_show.png,也就是选中时的图案;

<item android:id="@+android:id/background" android:drawable="@drawable/rating" />  为设置背景图案为 rating.png,也就是为选中时的图案。


Demo 下载
原文地址:https://www.cnblogs.com/sharecenter/p/5620971.html