我想要改变
SeekBar
的颜色,准确地说,我想要改变被拖动以更改进度值的点的颜色。我知道可以通过更改 "colorAccent" 的值来实现,但这不是我想做的。您所提到的点被称为SeekBar拇指。要更改SeekBar拇指的颜色,请在style.xml
中创建一个新样式。
通过XML进行更改
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumbTint="your_color" //gives your color
android:thumb="@drawable/yourseekthumb" //gives your drawable
android:theme="@style/SeekBarColor" />
您可以使用 thumbTint
或 android:thumb
来指定颜色或绘制对象。
此方法适用于API> 16。
要通过Java代码更改SeekBar拇指颜色
SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
seekbar.getThumb().setColorFilter(your_color, PorterDuff.Mode.MULTIPLY);
这个方法只适用于安卓5及以上版本。
尝试这个:只需在您的 SeekBar 中使用 android:thumbTint="@color/colorPrimary"
。
像这样:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:thumbTint="@color/red"
android:progress="0" />
Drawable seekbarThumb = getResources().getDrawable( R.drawable.seekbarThumb );
SeekBar mySeekBar = (SeekBar) findViewById(R.id.mySeekBar );
mySeekBar .setThumb(seekbarThumb );