java 如何在android中更改SeekBar颜色?(以编程方式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45329174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change SeekBar color in android? (Programmatically)
提问by Alpoe
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. 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);
回答by Abhi
To change the color of the Seekbarthumb, 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 Seekbarprogress 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().getCo??lor(R.color.your_color??), PorterDuff.Mode.SRC_ATOP);
回答by Georges daou
While using a style is a good idea, it does not provide much flexibility specially if you want to assign the colors programmatically I suggest:
虽然使用样式是个好主意,但如果您想以编程方式分配颜色,我建议它不会提供太多灵活性:
//for the progress
seekBar.getProgressDrawable().setColorFilter(mThemeColor,PorterDuff.Mode.SRC_ATOP);
//for the thumb handle
seekBar.getThumb().setColorFilter(mThemeColor, 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);
回答by twenk11k
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);
回答by Sachin Varma
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.
希望对未来的人有所帮助。
回答by ucMedia
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);
}
回答by Anmol317
You need to call you seekbar like this
你需要这样称呼你seekbar
SeekBar newSeek = new SeekBar(this, null, R.style.YOUR_NEW_STYLE);
where YOUR_NEW_STYLEwill define the colors of the Seekbarthat you want.
whereYOUR_NEW_STYLE将定义Seekbar您想要的颜色。
回答by Oussema Aroua
try this :
试试这个 :
seekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.mcolor), PorterDuff.Mode.MULTIPLY);
回答by Przemyslaw Jablonski
//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.
Find.setColorFilter()方法,使用一些 PorterDuff 过滤器将源颜色添加到其中,例如.Mode.MULTIPLY(取决于您最喜欢哪种过滤器模式),然后就可以了。
Example:
例子:
seekBar.setColorFilter(new PorterDuffColorFilter(srcColor,PorterDuff.Mode.MULTIPLY));

