Android 基础知识4-3.9 RatingBar(星级评分条)详解
一、引言
Android开发中,时不时的就有要实现星星的评分效果,比如某宝,某团,相信大家也都见过,当然了我们可以自己去画,也可以用美工给切的图去实现,其实在Android原生的控件中就可以来实现这样的效果,它就是RatingBar。
两种Style类型的星级平分效果代码,只在代码中简单引入:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><RatingBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:rating="5"style="?android:attr/ratingBarStyleIndicator"/><RatingBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:rating="3"style="?android:attr/ratingBarStyleSmall"/></LinearLayout>
效果图:
二、RatingBar简介
RatingBar是基于SeekBar和ProgressBar的扩展,用星型来显示等级评定。使用RatingBar的默认大小时,用户可以触摸/拖动或使用键来设置评分,它有两种样式(小风格用ratingBarStyleSmall,大风格用ratingBarStyleIndicator),其中大的只适合指示,不适合于用户交互。
当使用可以支持用户交互的RatingBar时,无论将控件(widgets)放在它的左边还是右边都是不合适的。
只有当布局的宽被设置为wrap content时,设置的星星数量(通过函数setNumStars(int)或者在XML的布局文件中定义)将显示出来(如果设置为另一种布局宽的话,后果无法预知)。
次级进度一般不应该被修改,因为他仅仅是被当作星型部分内部的填充背景。
三、事件处理
接口:RatingBar.OnRatingBarChangeListener
只需为RatingBar设置OnRatingBarChangeListener事件,然后重写下onRatingChanged()方法即可!
public class MainActivity extends AppCompatActivity {private RatingBar rb_normal;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);rb_normal = (RatingBar) findViewById(R.id.rb_normal);rb_normal.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {@Overridepublic void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),Toast.LENGTH_LONG).show();}});}
}
四、属性
android:isIndicator:是否用作指示,用户无法更改,默认false
android:numStars:显示多少个星星,必须为整数
android:rating:默认评分值,必须为浮点数
android:stepSize: 评分每次增加的值,必须为浮点数
除了上面这些,还有两种样式供我们选择咧,但是不建议使用,因为这两种样式都好丑... 他们分别是(上述效果图以及Xml代码中都有体现):
style="?android:attr/ratingBarStyleSmall"
style="?android:attr/ratingBarStyleIndicator"
五、公共方法
5.1、 public int getNumStars ()
返回显示的星型数量
5.2、public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()
返回值:监听器(可能为空)监听评分改变事件
5.3、public float getRating ()
获取当前的评分(填充的星型的数量)
5.4、public float getStepSize ()
获取评分条的步长
5.5、public boolean isIndicator ()
返回值:判断当前的评分条是否仅仅是一个指示器(注:即能否被修改)
5.6、public void setIsIndicator (boolean isIndicator)
设置当前的评分条是否仅仅是一个指示器(这样用户就不能进行修改操作了)
参数:isIndicator Bool值,是否是一个指示器
5.7、public synchronized void setMax (int max)
设置评分等级的范围,从0到max
参数:max 评分条最大范围。
5.8、public void setNumStars (int numStars)
设置显示的星型的数量。为了能够正常显示它们,建议将当前widget的布局宽度设置为
wrap content
参数:numStars 星型的数量
5.9、public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)
设置当评分等级发生改变时回调的监听器
参数:listener 监听器
5.10、public void setRating (float rating)
设置分数(星型的数量)
参数:rating 设置的分数
5.11、public void setStepSize (float stepSize)
设置当前评分条的步长(step size)
参数:stepSize 评分条的步进。例如:如果想要半个星星,它的值为0.5。
六、评分条RatingBar自定义设置
6.1、自定义颜色
这种方式也很简单,只需要要定义一个样式即可,两步完成。
第一步,定义样式,指定背景色 和 进度色。
<!--自定义RatingBar Color--><style name="RatingBar_CustomColor" parent="@android:style/Widget.Holo.RatingBar.Indicator"><!--Background Color--><item name="colorControlNormal">#D7D7D7</item><!--Progress Color--><item name="colorControlActivated">#FF0000</item></style>
第二步,XML中使用该主题。
<RatingBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="16dp"android:rating="3"style="?android:attr/ratingBarStyleIndicator"android:theme="@style/RatingBar_CustomColor"/>
效果图:
6.2、定制星级评分的背景图
我们可以将默认的星形图标换成上述笑脸形的。
接下来,编写一个layer-list的文件
ratingbar_full.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/ic_rating_off1" /><item android:id="@android:id/secondaryProgress"android:drawable="@mipmap/ic_rating_off1" /><item android:id="@android:id/progress"android:drawable="@mipmap/ic_rating_on1" />
</layer-list>
接着在res/values/themes.xml中自定义下RatingBar Style,在style.xml加上这个:
<style name="roomRatingBar" parent="@android:style/Widget.RatingBar"><item name="android:progressDrawable">@drawable/ratingbar_full</item><item name="android:minHeight">24dip</item><item name="android:minWidth">24dip</item></style>
最后在布局中的Ratingbar组件设置下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><RatingBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:rating="3"style="@style/roomRatingBar"/></LinearLayout>
效果图:
好的,效果还可以哈,至于间距问题,就需要对图片进行处理了,就是需要切图的时候在图片左右预留点空格就OK~
Android 基础知识4-3.9 RatingBar(星级评分条)详解相关推荐
- Android 基础知识4-2.11 AbsoluteLayout(绝对布局)详解
一.引言 Android中的五大布局,在本节中会讲解第六个布局AbsoluteLayout(绝对布局),之所以把这个放到最后,是因为AbsoluteLayout(绝对布局)我们基本上都是不会使用.当然 ...
- Android 基础知识4-2.8 TableLayout(表格布局)详解
一.TableLayout的概述 表格布局是以行数和列数来确定位置进行排列.就像一间教室,确定好行数与列数就能让同学有序入座. 注意:我们需要先添加<TableRow容器,每添加一个就会多一行, ...
- Android基础入门教程——2.3.1 TextView(文本框)详解
Android基础入门教程--2.3.1 TextView(文本框)详解 标签(空格分隔): Android基础入门教程 本节引言: 学习完Android中的六大布局,从本节开始我们来一个个讲解And ...
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
拖动条采用拖动滑块的位置来表示数值 SeekBar的常用xml属性值: 重要的android:thumb制定一个Drawable对象,改变滑块外观 通过滑块来改变图片的透明度: main.xml &l ...
- 【基础知识】深度学习中各种归一化方式详解
本文转载自 https://blog.csdn.net/qq_23981335/article/details/106572171 仅作记录学习~ 总结 BN,LN,IN,GN,WS 从学术上解释差异 ...
- 诚之和:Java基础知识枚举Enum类介绍以及案例使用详解
Java语言中的数据类型可以分为两大类,分别是基本数据类型和引用数据类型.本篇文章要介绍的枚举,就是属于Java的引用数据类型.下面,将为大家详细介绍Java中的枚举,以及具体的使用案例. 一.文章序 ...
- 【音视频第6天】基础知识-移动端实时音视频直播技术详解和开源工程WebRTC的技术原理和使用浅析
本文是系列文章中的第1篇,本系列文章的大纲如下: <移动端实时音视频直播技术详解(一):开篇> <移动端实时音视频直播技术详解(二):采集> <移动端实时音视频直播技术详 ...
- Android基础知识:在UI线程中运行代码
本文翻译自:Android basics: running code in the UI thread In the viewpoint of running code in the UI threa ...
- 100天精通Andriod逆向——第2天:Android基础知识和jadx的使用
目录 一.Android基础知识介绍 1.1 Android 历史版本 1.2 apk 包文件结构 1.3 Android系统目录介绍 二.jadx的使用 2.1 jadx 的简介 2.2 jadx ...
- Android基础知识(二十):Notification、提醒式通知(横幅)踩坑与通知界面设置跳转
Android基础知识(二十):Notification.提醒式通知(横幅)踩坑与通知界面设置跳转 一.Notification通知与基本用法 通知Notification是Android系统中比较有 ...
最新文章
- 万年历的设计c语言,万年历设计报告
- android远程控制灯光,智能灯具如何实现远程控制技术
- ArcGIS API for JavaScript 4.4学习笔记[新] AJS4.4和AJS3.21新特性
- CSS设置段落的垂直对齐
- 安全扫描工具​Nmap引擎理解文档
- Bug思路不清晰严谨
- [翻译] WindowsPhone-GameBoy模拟器开发二--Rom文件分析
- react context_使用React Context API-入门
- python3-pandas 缺失数据的处理
- 二.激光SLAM框架学习之A-LOAM框架---介绍及其演示
- UVA12657 Boxes in a Line【模拟】
- Adobe Flash CS4 序列号-Adobe Dreamweaver CS4 序列号
- c++语言判断是否质数,怎样用C++程序判断一个数是否为素数
- 【shenyu网关学习】1.什么是 Apache ShenYu
- mysql 如何分组统计个数_mysql 怎样统计分组数
- Idea 报错: Variable used in lambda expression should be final or effectively final
- ubuntu文件系统字体底纹含义
- 数据分析面试、笔试题汇总+解析(二)
- HbuilderX下载安装
- JavaWeb.购物车项目
热门文章
- windows10清除弹框广告
- activemq源码笔记:main函数小结
- 互联网大厂入局“虚拟偶像”:元宇宙里追星,是“庄周梦蝶”?
- 基于JAVA足球赛会管理系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
- 初次使用富文本编辑器,百度Ueditor显示图文翻译~
- 6 - PyQt5 基类 QObject
- VirtualBox 错误代码0x80004005解决办法
- 【免费】C# 窗口程序 制作简易定时关机 自动关机
- javaのチェーンリスト(鎖表)「链表」(基本)_JP
- 传统IDC部署网站10