项目里面最近很多客户端自定义的按钮形状跟底色要从服务器返回,今天测试遇到一问题,记录一下下:
错误的写法:
GradientDrawable gradientDrawable = ((GradientDrawable)((TextView)helper.getView(R.id.tv_subject)).getBackground());
if(null !=item.bcolor && !TextUtils.isEmpty(item.bcolor)){
int color = Color.parseColor(item.bcolor);
gradientDrawable.setStroke(1,color);
gradientDrawable.setColor(color);
}
正确写法:
StateListDrawable background = (StateListDrawable) ((TextView)helper.getView(R.id.tv_subject)).getBackground();
Drawable current = background.getCurrent();
if(current instanceof GradientDrawable){
GradientDrawable gradientDrawable= (GradientDrawable) current;
if(null !=item.bcolor && !TextUtils.isEmpty(item.bcolor)){
int color = Color.parseColor(item.bcolor);
gradientDrawable.setStroke(1,color);
gradientDrawable.setColor(color);
}
}
虽然还是不明白为什么要这么做,但是不这么做测试还是不行。。。。