总觉得这个控件有点陌生,先记下来。
实现效果:
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 指定滑块背景。
总觉得这篇博文不够简练,各位见谅。