Android 动态修改SeekBar滑块和进度条的颜色

本文详细介绍如何通过XML资源文件和代码方式自定义Android中SeekBar的颜色、样式。包括滑竿和滑块的样式修改,以及如何在布局中引用这些自定义样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一

1. 需求:需要改变其默认颜色,样式

2.滑竿样式

seekbar.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:height="5dp">
        <shape>
            <corners android:radius="5dp"/>
            <solid android:color="#C9C7C7"/>           <!--  颜色-->
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress" android:height="5dp">
        <clip>
            <shape>
                <corners android:radius="5dp"/>
                <solid android:color="#C9C7C7"/>            <!-- 颜色-->
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress" android:height="5dp">
        <clip>
            <shape>
                <corners android:radius="5dp"/>
                <solid android:color="@color/colorPrimary"/>   <!--  颜色-->
            </shape>
        </clip>
    </item>
</layer-list>

复制代码

3. 滑块样式

select_seekbar.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/shape1"/>

    <!-- 有焦点状态 -->
    <item  android:state_focused="true"  android:drawable="@drawable/shape"/>

    <!-- 普通状态 -->
    <item android:drawable="@drawable/shape" />
</selector>

复制代码

 

shape.xml

复制代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="@color/colorPrimary"></solid>  <!-- 颜色-->
    <size
        android:width="12dp"
        android:height="12dp"></size>
</shape>

复制代码

shap1.xml

复制代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="@color/colorAccent"></solid> <!-- 颜色-->
    <size
        android:width="14dp"
        android:height="14dp"></size>
</shape>

复制代码

4.布局引用

复制代码

<SeekBar
    android:id="@+id/seekbar"
    android:layout_width="368dp"
    android:maxHeight="6dip"
    android:minHeight="6dip"
    android:layout_height="40dp"
    android:max="100"
    android:progressDrawable="@drawable/seekbar"
    android:thumb="@drawable/select_seekbar"
    android:progress="30" />

<!--maxHeight
    minHeight可以控制滑竿粗线-- >

复制代码

方法二

5.不利用xml,直接修改

复制代码

 public void setSeekBarColor(SeekBar seekBar, int color){
        LayerDrawable layerDrawable = (LayerDrawable)        
seekBar.getProgressDrawable();
        Drawable dra=layerDrawable.getDrawable(2);
        dra.setColorFilter(color, PorterDuff.Mode.SRC);
        Drawable dra2=seekBar.getThumb();
        seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        seekBar.invalidate();
    }

复制代码

 

6.效果图

 

 

转载自:https://www.cnblogs.com/galibujianbusana/p/7066168.html 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值