RatingBar使用

简介: RatingBar使用

RatingBar是ProgressBar的一个延伸,ProgressBar的进度一般由代码控制,RatingBar的进度可以拖拽来控制。

默认效果如下:

image.png

163.gif

看到这个效果图之后,您可能会想到打车软件和订餐软件上打五星好评的UI了吧,RatingBar可以完美实现这个效果。

(1)监听

监听RatingBar进度情况的代码如下:

    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        }
    });

rating参数为当前进度值。

(2)RatingBar的4个属性

  • android:isIndicator

RatingBar的指示是否起作用,默认为false,如果为true,RatingBar的进度不能被人为修改。

  • android:numStars

显示星星的总数量,必须为整数。

  • android:rating

设置默认评分值,值为浮点数。

  • android:stepSize

评分每次增加的值(最小变化值),值为浮点数。

(3)系统样式

image.png

图片.png

系统样式有三种:

  • 默认样式
    style="?attr/ratingBarStyle"

    style="@style/Widget.AppCompat.RatingBar"
  • 指示器样式
    style="?attr/ratingBarStyleIndicator"

    style="@style/Widget.AppCompat.RatingBar.Indicator"
  • 小型样式
    style="?attr/ratingBarStyleSmall"

    style="@style/Widget.AppCompat.RatingBar.Small"

(4)progressDrawable的使用

系统无样式往往还无法满足大部分需求,如果需要将星星换成其它图案呢?

使用progressDrawable属性,设置RatingBar的背景样式。效果图如下:

image.png

图片.png

代码如下:

    android:progressDrawable="@drawable/ratingbar_bg"

ratingbar_bg.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/xiong_off"/>
    <item
        android:id="@android:id/progress"
        android:drawable="@mipmap/xiong_on"/>
</layer-list>


相关文章
|
5月前
|
Android开发
[Android]DrawerLayout滑动菜单+NavigationView
[Android]DrawerLayout滑动菜单+NavigationView
27 0
|
11月前
|
XML 数据格式
DrawerLayout侧滑菜单、Toolbar和沉浸式状态栏的使用
DrawerLayout侧滑菜单、Toolbar和沉浸式状态栏的使用
|
XML Java Android开发
TextInputLayout 使用
TextInputLayout 使用
80 0
|
Android开发
DrawerLayout使用详解
DrawerLayout使用详解
263 0
|
数据安全/隐私保护
TextInputLayout使用详解
TextInputLayout使用详解
323 0
TextInputLayout使用详解
|
XML Android开发 数据格式
TextInputLayout详解
TextInputLayout是什么 TextInputLayout主要是作为EditText的容器,从而为EditText生成一个浮动的Label,当用户点击EditText的时候,EditText中的hint字符串会自动移到EditText的左上角。
946 0