ProgressBar的背景设置xml时可以使用,但代码setProgressDrawable设置则无效。
layer-list如下:
<?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:drawable="@drawable/icon_love">
</item>
<item
android:id="@+android:id/secondaryProgress"
android:drawable="@drawable/icon_love">
</item>
<item
android:id="@+android:id/progress"
android:drawable="@drawable/icon_love">
</item>
</layer-list>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
在代码设置无效:
ProgressBar.setProgressDrawable(getResources().getDrawable(R.drawable.ratingbar_drawable_heart))
查看源码发现:
public void setProgressDrawable(Drawable d) {
boolean needUpdate;
if (mProgressDrawable != null && d != mProgressDrawable) {
mProgressDrawable.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (d != null) {
d.setCallback(this);
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
}
mProgressDrawable = d;
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
导致相当于换了背景一样(本来有三颗星的):
![这里写图片描述](https://img-blog.csdn.net/20170321170602828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmxvdmUx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
再发现sdk版本21有setProgressDrawableTiled方法可以解决,最版本怎么处理?原来setProgressDrawableTiled有个方法tileify:
public void setProgressDrawableTiled(Drawable d) {
if (d != null) {
d = tileify(d, false);
}
setProgressDrawable(d);
}
是否底版本也有,看了一下14版本的sdk,也有这个私有方法!
private Drawable tileify(Drawable drawable, boolean clip) {
if (drawable instanceof LayerDrawable) {
LayerDrawable background = (LayerDrawable) drawable;
final int N = background.getNumberOfLayers();
Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
int id = background.getId(i);
outDrawables[i] = tileify(background.getDrawable(i),
(id == R.id.progress || id == R.id.secondaryProgress));
}
LayerDrawable newBg = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
newBg.setId(i, background.getId(i));
}
return newBg;
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
那解决方法可想而知,用java反射调用此方法:
@SuppressLint("NewApi")
public static void setProgressDrawable(@NonNull ProgressBar bar, @DrawableRes int resId) {
Drawable layerDrawable = bar.getResources().getDrawable(resId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable d = getMethod("tileify", bar, new Object[] { layerDrawable, false });
bar.setProgressDrawable(d);
} else {
bar.setProgressDrawableTiled(layerDrawable);
}
}
private static Drawable getMethod(String methodName, Object o, Object[] paras) {
Drawable newDrawable = null;
try {
Class<?> c[] = new Class[2];
c[0] = Drawable.class;
c[1] = boolean.class;
Method method = ProgressBar.class.getDeclaredMethod(methodName, c);
method.setAccessible(true);
newDrawable = (Drawable) method.invoke(o, paras);
} catch (Exception ex) {
ex.printStackTrace();
}
return newDrawable;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
最后调用此方法
setProgressDrawable(bar, R.drawable.ratingbar_drawable_heart);
解决底版本代码设置问题:
![这里写图片描述](https://img-blog.csdn.net/20170321173506669?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmxvdmUx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
ps:本文转载,谢谢欣赏!!