[英]how to change SeekBar color in android? (Programmatically)
I made an equalizer to go with my app but I am not sure how I can change the seekbar's thumb and progress color.我用我的应用程序为 go 制作了一个均衡器,但我不确定如何更改搜索栏的拇指和进度颜色。 It seems to be pink by default and that doesn't fit my app's aesthetics.
默认情况下它似乎是粉红色的,这不符合我的应用程序的美学。
SeekBar seekBar = new SeekBar(this);
seekBar.setId(i);
seekBar.setLayoutParams(layoutParams);
seekBar.setMax(upperEqualizerBandLevel - lowerEqualizerBandLevel);
seekBar.setProgress(mEqualizer.getBandLevel(equalizerBandIndex));
//seekBar.setBackgroundColor(Color.DKGRAY);
//seekBar.setDrawingCacheBackgroundColor(Color.DKGRAY);
To change the color of the Seekbar
thumb, create a new style in style.xml
要更改
Seekbar
拇指的颜色,请在style.xml
创建一个新样式
<style name="SeekBarColor"
parent="Widget.AppCompat.SeekBar">
<item name="colorAccent">@color/your_color</item>
</style>
Finally in the layout:最后在布局中:
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/SeekBarColor" />
To change the Seekbar
progress color, use this in Java Class.要更改
Seekbar
进度颜色,请在 Java 类中使用它。
seekBar.getProgressDrawable().setColorFilter("yourcolor", PorterDuff.Mode.MULTIPLY);
Both these will work for API>16.这两个都适用于 API>16。
Edit编辑
To change SeekBar thumb color by Java code.通过 Java 代码更改 SeekBar 拇指颜色。
seekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_ATOP);
虽然使用样式是个好主意,但如果您想以编程方式分配颜色,我建议它不会提供太多灵活性:
//for the progress seekBar.getProgressDrawable().setColorFilter(mThemeColor,PorterDuff.Mode.SRC_ATOP); //for the thumb handle seekBar.getThumb().setColorFilter(mThemeColor, PorterDuff.Mode.SRC_ATOP);
You can change seekbar thumb and progress colors for programmatically like this:您可以像这样以编程方式更改搜索栏拇指和进度颜色:
seekBar.getProgressDrawable().setColorFilter(Utils.getAccentColor(this), PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Utils.getAccentColor(this), PorterDuff.Mode.SRC_IN);
You can easily change it via code,您可以通过代码轻松更改它,
For example:例如:
seekbar.setProgressTintList(ColorStateList.valueOf(Color.parseColor(#000000)));
or或
seekbar.setProgressTintList(ColorStateList.valueOf(Color.RED));
Hoping that it will be helpfull for someone in future.希望对未来的人有所帮助。
You need to call you seekbar like this你需要这样称呼你seekbar
SeekBar newSeek = new SeekBar(this, null, R.style.YOUR_NEW_STYLE);
where YOUR_NEW_STYLE
will define the colors of the Seekbar
that you want.其中
YOUR_NEW_STYLE
将定义您想要的搜索Seekbar
的颜色。
试试这个:
seekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.mcolor), PorterDuff.Mode.MULTIPLY);
Just a little more peachy answer只是多一点桃色的答案
public static void setSeekBarColor(SeekBar seekBar, int color) {
seekBar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
seekBar.getThumb().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
Kotlin version, and without using deprecated methods: Kotlin 版本,并且不使用已弃用的方法:
val colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
Resources.getSystem().getColor(R.color.your_color, null),
BlendModeCompat.SRC_ATOP
)
// SeekBar progress color
seekBar.progressDrawable.colorFilter = colorFilter
// SeekBar thumb handle color
seekBar.thumb.colorFilter = colorFilter
//seekBar.setBackgroundColor(Color.DKGRAY);
//seekBar.setDrawingCacheBackgroundColor(Color.DKGRAY);
There are few Views in Android SDK (ProgressBar is another example) in which you have to change colour using graphical color filters, instead of changing Background / Foreground source colour. Android SDK 中很少有视图(ProgressBar 是另一个示例),您必须使用图形颜色过滤器更改颜色,而不是更改背景/前景源颜色。
Find .setColorFilter()
method, supply your source colour into it with some PorterDuff filter, like .Mode.MULTIPLY
(depending on what filter mode you like best) and there you go.找到
.setColorFilter()
方法,使用一些 PorterDuff 过滤器将源颜色提供给它,例如.Mode.MULTIPLY
(取决于您最喜欢哪种过滤器模式),然后就可以了。
Example:示例:
seekBar.setColorFilter(new PorterDuffColorFilter(srcColor,PorterDuff.Mode.MULTIPLY));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.