Android学习笔记--如何修改SeekBar的样式

自定义SeekBar样式

谷歌是怎么定义的?

    <SeekBar 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        style="@android:style/Widget.SeekBar"/>

谷歌定义的SeekBar的样式全在@android:style/Widget.SeekBar中,通过样式可以观察发现谷歌是如何定义SeekBar的。可以通过修改样式,覆盖原来样式,从而达到我们需要的效果。

    <style name="Widget.SeekBar">
        <item name="indeterminateOnly">false</item>
        <!--progressDrawable定义了背景图,进度图,和缓冲图-->
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <!--thumb定义了滑块的图片-->
        <item name="thumb">@drawable/seek_thumb</item>
        <item name="thumbOffset">8dip</item>
        <item name="focusable">true</item>
        <item name="mirrorForRtl">true</item>
    </style>

如果想改变SeekBar的背景图,进度图,和缓存图等,我们可以自定义progressDrawable,例如
progress_horizotal.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--这是SeekBar的背景图-->
    <item android:id="@android:id/background" android:drawable="@drawable/seekbar_background">
    </item>
    <!-- 这是缓存进度图-->
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <!--这是SeekBar的进度图-->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <solid android:color="@color/blue"/>
            </shape>
        </clip>
    </item>

</layer-list>

然后在定义SeekBar的时候可以这样使用:

   <SeekBar
                style="@android:style/Widget.SeekBar"
                <!--覆盖原来进度的样式-->               android:progressDrawable="@drawable/progress_horizotal"
                <!--覆盖滑块的样式-->  
                android:thumb="@drawable/seekbar_thumb"
                android:minHeight="3dp"
                android:maxHeight="3dp"
                android:max="100"
                android:progress="50"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值