我创建了一个扩展ImageView的小型自定义视图。
我的自定义视图提供了一个方法showError(int)
,可以传递资源id,该资源id应显示为图像视图的内容。如果我能够传递简单的颜色资源id或可绘制资源id,那就太好了。
问题是:如何确定传递的资源id是Drawable还是Color?
我的当前方法类似于:
class MyImageView extends ImageView{
public void showError(int resId){
try{
int color = getResources().getColor(resId);
setImageDrawable(new ColorDrawable(color));
}catch(NotFoundException e){
// it was not a Color resource so it must be a drawable
setImageDrawable(getResources().getDrawable(resId));
}
}
}
这样做安全吗?我的假设是,资源ID确实是唯一的。我的意思不是在R.drawable或R.color中唯一,而是在R
中完全唯一。
因此,没有
R.drawable.foo_drawable = 1;
R.color.foo_color = 1;
这是正确的吗,即id 1
只会被分配给这两个资源中的一个而不是两个都有?