android学习笔记九——RatingBar
RatingBar==>星级评分条
RatingBar和SeekBar十分相似,它们甚至有相同的父类:AbsSeekBar.两者都允许用户通过拖动来改变进度;
两者最大的区别在于RatingBar是通过星星表示进度;RatingBar本身就是一个特殊的SeekBar。
RatingBar常见XML属性如下图:
注意:为了程序支持响应星级评分条的改变,程序可以考虑为其绑定一个OnRatingBarChangeLinstener监听器。
实例如下:
- 布局文件==》
- <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <!-- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="200px"
- android:src="@drawable/no" />-->
- <RatingBar
- android:id="@+id/ratingbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:max="255"
- android:numStars="5"
- android:progress="255"
- android:stepSize="0.5" />
- </RelativeLayout>
- 代码实现==》
- package com.example.myratingbar;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.ImageView;
- import android.widget.RatingBar;
- import android.widget.RatingBar.OnRatingBarChangeListener;
- public class MainActivity extends Activity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- RatingBar bar = (RatingBar) this.findViewById(R.id.ratingbar);
- //final ImageView img = (ImageView) this.findViewById(R.id.image);
- bar.setOnRatingBarChangeListener(new OnRatingBarChangeListener()
- {
- @SuppressWarnings("deprecation")
- @Override
- public void onRatingChanged(RatingBar ratingBar, float rating,
- boolean fromUser)
- {
- //img.setAlpha((int) (rating * 255 / 5));
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
实现效果如:
如何替换如上图系统默认星星图标,实现方式如下:
参考:http://my.oschina.net/amigos/blog/60060
drawable-mdpl资源文件==》
- <?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/unselect">
- </item>
- <item
- android:id="@+android:id/secondaryProgress"
- android:drawable="@drawable/unselect">
- </item>
- <item
- android:id="@+android:id/progress"
- android:drawable="@drawable/select">
- </item>
- </layer-list>
样式文件==》style.xml
- <style name="roomRatingBar" parent="@android:style/Widget.RatingBar">
- <item name="android:progressDrawable">@drawable/ratingbar_drawable</item>
- <item name="android:minHeight">48dip</item>
- <item name="android:maxHeight">48dip</item>
- </style>
加 布局文件==》
- <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" >
- <RatingBar
- android:id="@+id/ratingbar"
- style="@style/roomRatingBar"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dip"
- android:numStars="8"
- android:rating="1.5"
- android:stepSize="0.15" />
- </LinearLayout>
运行效果如下:
android学习笔记九——RatingBar的更多相关文章
- Android学习笔记九:Service
一:Service是什么 Service,服务.一般用于提供需要在后台长期运行的服务(如复杂计算.下载等等耗时任务),其特点是长生命周期的.没有用户界面.在后台运行的. 二:Service的生命周期方 ...
- 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...
- 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...
- 【转】 Pro Android学习笔记(六九):HTTP服务(3):HTTP POST MultiPart
目录(?)[-] 建立测试环境 开发环境导入第三方JAR HTTP Post Multipart小例子 HTTP POST不仅可以通过键值对传递参数,还可以携带更为复杂的参数,例如文件.HTTP Po ...
- 【转】 Pro Android学习笔记(二九):用户界面和控制(17):include和merge
目录(?)[-] xml控件代码重用include xml控件代码重用merge 横屏和竖屏landsacpe portrait xml控件代码重用:include 如果我们定义一个控件,需要在不同的 ...
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
- 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件
目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...
- 【转】Pro Android学习笔记(二五):用户界面和控制(13):LinearLayout和TableLayout
目录(?)[-] 布局Layout 线性布局LinearLayout 表格布局TableLayout 布局Layout Layout是容器,用于对所包含的view进行布局.layout是view的子类 ...
- 【转】 Pro Android学习笔记(二二):用户界面和控制(10):自定义Adapter
目录(?)[-] 设计Adapter的布局 代码部分 Activity的代码 MyAdapter的代码数据源和构造函数 MyAdapter的代码实现自定义的adapter MyAdapter的代码继续 ...
随机推荐
- (实用篇)PHP ftp上传文件操作类
<?php /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) */ class class_ftp { public $off; // 返回操作状态(成功/失败) publi ...
- JS中onpropertychange和onchange事件区别小结
当一个HTML元素的属性用js改变的时候,都能通过onpropertychange来捕获.例如一个文本text对象的value属性被页面的脚本修改的时候,onchange无法捕获到,而onproper ...
- Oracle执行语句跟踪(2)——使用10046事件实现语句追踪
接上篇博文Oracle执行语句跟踪(1)--使用sql trace实现语句追踪,一旦我们通过会话追踪获取到超时事物的执行语句,就可以使用10046事件对语句进行追踪. 启用10046事件追踪的方式 S ...
- C++ list<list<int> >类型的对象遍历
void listSort(list<list<int> >* initList) { list<list<int> >::iterator itera ...
- 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- SQL server 2008 数据库企业版安装教程图解
SQL Server 2008是一个重大的产品版本,它推出了许多新的特性和关键的改进,使得它成为至今为止的最强大和最全面的SQL Server版本. 在现今数据的世界里,公司要获得成功和不断发展,他 ...
- iframe session过期跳转到登陆页面
在login.jsp中添加js: if(window !=top){ top.location.href=location.href; } <script type="text/jav ...
- magic矩阵
魔方矩阵 魔方矩阵是有相同的行数和列数,并在每行每列.对角线上的和都相等.你能构造任何大小(除了2x2)的魔方矩阵. 1.历史 魔方又称幻方.纵横图.九宫图,最早记录于我国古代的洛书.据说 ...
- apache开启url rewrite模块
在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...
- ps命令详解(转)
原文地址:http://apps.hi.baidu.com/share/detail/32573968 有时候系统管理员可能只关心现在系统中运行着哪些程序,而不想知道有哪些进程在运行.由于一个应用程序 ...