最近项目中有用到seekbar,之前对这东西不太了解,趁机来踩坑。
seekbar样式
按我觉得Material 中的还不算难看了。但是美工给了自己的样式,还是得改。
主要有这2个属性:
android:thumb="@drawable/thumb"
android:progressDrawable="@drawable/seekbar_progress"
thumb 指的是进度条上那个滑块,progressDrawable 指的是进度条。
seekbar这是progressDrawable :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#bfbfbf" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#4765e5" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#4765e5" />
</shape>
</clip>
</item>
</layer-list>
这里说一下,android:id="@android:id/background" 背景指的是上图中灰色的部分。android:id="@android:id/progress"是蓝色部分。自己一开始把secondaryProgress 和background 给搞混了。
thumb的话就是简单的一张图片,再指定一下大小,seekbar默认是有10dp 的height的。
android:maxHeight="5dip"
android:minHeight="5dip"
这样大概就能得到图中的样子了。
然后,最后。跑起来发现滑块thumb四周竟然不是透明的。扯蛋! 赶紧去看了一下icon 发现icon四周是透明的没错。又一阵蛋疼。查一下发现,原来 The Material seek bar has split track enabled by default
所以我们要把它disable掉。
android:splitTrack="false"
Over
网友评论
This check finds attributes set in XML files that were introduced in a version newer than the oldest version targeted by your application (with the minSdkVersion attribute). This is not an error; the application will simply ignore the attribute. However, if the attribute is important to the appearance of functionality of your application, you should consider finding an alternative way to achieve the same result with only available attributes, and then you can optionally create a copy of the layout in a layout-vNN folder which will be used on API NN or higher where you can take advantage of the newer attribute. Note: This check does not only apply to attributes. For example, some tags can be unused too, such as the new <tag> element in layouts introduced in API 21.
```
<SeekBar
android:id="@+id/sb_seek_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp16"
android:layout_marginRight="@dimen/dp16"
android:layout_weight="1"
android:maxHeight="@dimen/dp3"
android:minHeight="@dimen/dp3"
android:paddingEnd="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingStart="0dp"
android:progressDrawable="@drawable/seek_bar_progress"
android:thumb="@drawable/seek_bar_thumb"
android:thumbOffset="0dp" />
```