棒棒糖进度栏着色 [英] Lollipop Progress Bar Tinting
本文介绍了棒棒糖进度栏着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Lollipop设备上(MotoG 2014),我读到有关进度条着色的信息,但这不起作用...我得到了默认的进度条颜色.我在这里想念什么?
I'm on a Lollipop device (MotoG 2014), I read about progress bar tinting, but this is not working...I get the default progress bar color. What am I missing here?
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="50dp"
android:layout_height="50dp"
android:backgroundTintMode="src_in"
android:indeterminate="true"
android:indeterminateTint="#f00" />
非常感谢.
推荐答案
在Lollipop之前的版本中,可接受的解决方案不适用于我,但是我发现该解决方案适合所有API,但最重要的是,使用任何不推荐使用的代码:
The accepted solution wasn't working for me on pre-Lollipop, but I found this solution to fit all APIs and on top of that, it doesn't use any deprecated code:
Java解决方案
// fixes pre-Lollipop progressBar indeterminateDrawable tinting
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}
Kotlin扩展
fun ProgressBar.setIndeterminateTintCompat(@ColorInt color: Int) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
val wrapDrawable = DrawableCompat.wrap(indeterminateDrawable)
DrawableCompat.setTint(wrapDrawable, color)
indeterminateDrawable = DrawableCompat.unwrap(wrapDrawable)
} else {
indeterminateTintList = ColorStateList.valueOf(color)
}
}
// usage
val desiredColor = ContextCompat.getColor(context, R.color.myColor)
myProgressBar.setIndeterminateTintCompat(desiredColor)
编码愉快!
这篇关于棒棒糖进度栏着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文