Android常用控件用法--SeekBar

SeekBar 

   最近在开发项目时,其中用到了控件SeekBar,觉得它的用法比较多,有必要做一个记录,以备以后快速上手。

   常常需要自定义已列出,直接上用法

  ----1---- android:thumb:自定义滑块。

 <SeekBar
                android:id="@+id/sound"
                android:layout_width="150px"
                android:layout_height="10px"
                android:max="100"  //设置拖动条最大值
                android:progress="10"   //设置拖动条当前值
                android:progressDrawable="@layout/seekbar_style"  //拖动条样式
                android:thumb="@layout/thumb" />    //滑块样式

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="@drawable/thumb1"      
        />      
                
    <!-- 普通无焦点状态 -->    
    <item       
        android:state_focused="false"       
        android:state_pressed="false"     
        android:drawable="@drawable/thumb2"  
    />   
</selector>    

---2-----拖动条的高度----------------

设置高度的时候一定要用

  android:layout_height="wrap_content"
  android:maxHeight="6px"
  android:minHeight="6px"

---3---- 拖动条的颜色----------------

android:progressDrawable="@layout/seekbar_style" 

seekbar_style.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>
            <corners android:radius="5dp" />
            <solid android:color="#ffacacac"/>
        </shape>
    </item>
    <item android:id="@android:id/progress"  >
        <clip>
            <shape>
                <corners android:radius="5dp" />
                <solid android:color="#00bbff"/>
            </shape>
        </clip>
    </item>
</layer-list>

-----4------增大拖动条的触摸范围(热区范围)------

 // 增大view的可点击范围
    public  void addDefaultScreenArea(final View view, final int top,final int bottom,final int left,final int right) {
        Log.v("AudioSeekbarActivity2", "----->>>addDefaultScreenArea = ");
        final View parent = (View) view.getParent();
        parent.post(new Runnable() {
            public void run() {
                Rect bounds = new Rect();
                view.setEnabled(true);
                view.getHitRect(bounds);
                bounds.top -= top;
                bounds.bottom += bottom;
                bounds.left -= left;
                bounds.right += right;
                TouchDelegate touchDelegate = new TouchDelegate(bounds, view);
                if (View.class.isInstance(view.getParent())) {
                    ((View) view.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值