自定义SeekBar(简单点的)

来源:互联网 发布:电脑360数据恢复大师 编辑:程序博客网 时间:2024/05/21 10:59

1.SeekBar的基本属性

android:max="100" //滑动条的最大值android:progress="60" //滑动条的当前值android:secondaryProgress="70" //二级滑动条的进度android:thumb = "@mipmap/sb_icon" //滑块的drawable

2.SeekBar的监听事件

setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {            @Override            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {                txt_cur.setText("当前进度值:" + progress + "  / 100 ");                //进度值发生改变时会触发            }            @Override            public void onStartTrackingTouch(SeekBar seekBar) {                Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();                //按住进度条时会触发            }            @Override            public void onStopTrackingTouch(SeekBar seekBar) {                Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();                //发开进度条时会触发            }        });

3.SeekBar的简单自定义
效果图:
这里写图片描述
1)滑块的drawable:sb_thumb.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true" android:drawable="@mipmap/seekbar_thumb_pressed"/>    <item android:state_pressed="false" android:drawable="@mipmap/seekbar_thumb_normal"/></selector>

2)条形栏Bar的Drawable:sb_bar.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">        <shape>            <solid android:color="#FFFFD042" />        </shape>    </item>    <item android:id="@android:id/secondaryProgress">        <clip>            <shape>                <solid android:color="#FFFFFFFF" />            </shape>        </clip>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <solid android:color="#FF96E85D" />            </shape>        </clip>    </item></layer-list>

3)然后布局引入SeekBar后,设置下progressDrawable与thumb即可!

<SeekBar        android:id="@+id/sb_normal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:maxHeight="5.0dp"        android:minHeight="5.0dp"        android:progressDrawable="@drawable/sb_bar"        android:thumb="@drawable/sb_thumb"/>

参考文章:http://www.runoob.com/w3cnote/android-tutorial-seekbar.html

0 0
原创粉丝点击