星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似。实际上星际评分条与拖动条的用法、功能都十分接近:它们都允许用户通过拖动来改变进度。RatingBar与SeekBar的最大区别在于:RatingBar通过星星来表示进度。

RatingBar支持的常用XML属性

XML属性

说明

android:isIndicator

设置该星级评分条是否允许用户改变(true为不允许修改)

android:numStars

设置该星机评分条总共有多少个星级

android:rating

设置该星级评分条默认的星级

android:stepSize

设置每次最少需要改变多少个星级

为了让程序能响应星级评分条评分的改变,可以考虑为它绑定一个OnRatingBarChangeListener监听器。

下面通过实例来示范RatingBar的功能与用法。

实例:通过星级改变图片的透明度

界面布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="240dp"
android:src="@drawable/lijiang"/>
<!--定义一个拖动条,并改变它的滑块外观-->
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:max="255"
android:progress="255"
android:stepSize="0.5"/>
</LinearLayout>

上面的布局文件中指定了该星际评分条的最大值为255,当前进度为255。其中两个属性都来自ProgressBar组件,这没有任何问题,因为RatingBar本来就是一个特殊的ProgressBar。

主程序只用为RatingBar绑定事件监听器,即可监听星级评分条的星级改变。主程序如下

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView image = findViewById(R.id.image);
RatingBar ratingBar = findViewById(R.id.rating);
ratingBar.setOnRatingBarChangeListener((bar, rating, fromUser)->{
//当星级评分条的评分发生改变时触发该方法
//动态改变图片的透明度,其中255是星级评分条的最大值
//5颗星星就代表最大值255
image.setImageAlpha((int)(rating * 255 / 5));
});
}
}

上面定义了RatingBar时指定了android:stepSize="0.5",由此改星级评分条中星级的最小改变值为0.5,最少半个星级。

运行程序截图如下

最新文章

  1. SILVERLIGHT 应急卫生模拟演练项目之loading界面实现
  2. css3新特性@rgba
  3. [Linux技巧]固定Vmware下CentOS的IP
  4. hdu 2041:超级楼梯(水题,递归)
  5. [转]利于ThreadLocal管理Hibernate Session
  6. Codeforces Round #368 (Div. 2) D. Persistent Bookcase
  7. JavaWeb项目开发案例精粹-第3章在线考试系统-005action层
  8. 使用LINQ 對List分頁/區
  9. linux命令行模式下实现代理上网
  10. c# winform textbox与combox让用户不能输入
  11. Painting The Wall 期望DP Codeforces 398_B
  12. Flash Vector例子
  13. linux学习之四---gdb调试
  14. iOS设置UITableView中Cell被默认选中后怎么触发didselect事件
  15. nyoj281 整数中的1(二) 数位DP
  16. hdu 2888 二维RMQ模板题
  17. WebApplicationContext初始化
  18. Django视图层、虚拟环境
  19. 【DDD】使用领域驱动设计思想实现业务系统
  20. Spring异步调用注解@Async的使用

热门文章

  1. .netcore+vue+elementUI 前后端分离---支持前端、后台业务代码扩展的快速开发框架
  2. 利用Helm简化Kubernetes应用部署(1)
  3. PID算法的理解及场景模拟
  4. HTTP/1.1与HTTP/2有什么区别?
  5. JZOJ10004 列车调度
  6. mysql锁表处理语句
  7. Head First设计模式——观察者模式
  8. nishang的介绍与使用
  9. DevSecOps 笔记
  10. java集合之Stack栈基础