SeekBar使用
布局声明 <SeekBar>
定义OnSeekBarChangeListener
实现其中onProgressChanged,onStartTrackingTouch,onStopTrackingTouch
注意onProgressChanged中有一个boolean fromUser参数
绑定监听器
RatingBar使用
布局声明
<RatingBar> 其中有2个特别属性 android:numStars="5"(星数) android:stepSize="1.0"(每次进多少)
定义OnRatingBarChangeListener
实现onRatingChanged 同样包含fromUser参数
绑定监听器
视频里讲得非常简单 但是往往实际操作中 对很多控件的样式有比较高的要求 正好简单用到过一些 作为补充一下
SeekBar为例
最简单的 在布局文件中通过属性定义
android:progressDrawable="@drawable/bar_style" //背景
android:thumb="@drawable/bar" //拖动的按钮
android:thumbOffset="0dip" //如果不是0的话 游标则不是从头开始 会比较奇怪 根据需要设置
而之后如果仍然不能满足要求 可以考虑通过修改drawable selector等进行进一步的定制
例如定义一个自己的drawable作为背景
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff0099CC"
android:centerColor="#ff3399CC"
android:centerY="0.75"
android:endColor="#ff6699CC"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
也可以直接使用预先准备好的图片
<?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/progress_bg" />
<item android:id="@android:id/secondaryProgress"
android:drawable="@drawable/second_progress">
</item>
<item android:id="@android:id/progress"
android:drawable="@drawable/first_progress">
</item>
</layer-list>
也可以对各种状态通过selector进行定义 同样适用于按钮之类的其他控件
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 按下状态-->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/thumb_pressed" />
<!-- 普通无焦点状态 -->
<item
android:state_focused="false"
android:state_pressed="false"
android:drawable="@drawable/thumb_normal" />
<!-- 有焦点状态-->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/thumb_focused" />
<!-- 有焦点 -->
<item
android:state_focused="true"
android:drawable="@drawable/thumb_focused" />
</selector>
以上图片和xml文件 都放在drawable文件夹下 通过R.drawable.xxx或者在布局中@drawable/xxx.xml等方式引用
code来自于http://www.devdiv.com/thread-46488-1-1.html
本文详细介绍了SeekBar和RatingBar这两个Android控件的使用方法,包括如何在布局文件中声明、定义监听器及实现监听器的方法。此外,还提供了SeekBar样式定制的实例,帮助开发者更好地调整控件外观。
2014

被折叠的 条评论
为什么被折叠?



