Android 自定义SeekBar控件

总觉得这个控件有点陌生,先记下来。


实现效果:



1. 先大致预览一下谷歌提供的 SeekBar 背景源码(holo主题):

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
            android:drawable="@android:drawable/scrubber_track_holo_light" />
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%"
               android:drawable="@android:drawable/scrubber_secondary_holo" />
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%"
               android:drawable="@android:drawable/scrubber_primary_holo" />
    </item>
</layer-list>

值 @android:id/background 指定上图灰色的背景;

值 @android:id/prgress 指定当前进度背景

@android:id/secondaryProgress 指定第二进度背景,可以忽略,可同 @android:id/prgress 或 @android:id/background 值一致

属性 android:scaleWidth 指定缩放比例,这里设置为100%,否则进度条背景将被覆盖。如图所示:




2.自定义SeekBar背景 seekbar_bg.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="@drawable/seek_default_total" />
    
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%" 
        	android:drawable="@drawable/seek_default_total" />
    </item>
    
    <item android:id="@android:id/progress" >
        <scale android:scaleWidth="100%"
        	android:drawable="@drawable/seek_default_progress" />
    </item>
</layer-list>

3.准备滑块图片资源,笔者把上面的滑块贴出来:


4.使用自定义 SeekBar 控件:

<SeekBar
	android:id="@+id/SoxfmrSeekBar"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:progressDrawable="@drawable/seek_bg"
	android:thumb="@drawable/seek_thumb_bg" />
属性 android:progressDrawable 指定进度条背景;

属性 android:thumb 指定滑块背景。


总觉得这篇博文不够简练,各位见谅。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值