我有一个代码片段要在下面显示,我正在尝试更改视图的(v)背景。我将从TextView
(拖动的)获取颜色代码并View
使用此代码更改(v)的背景。但我收到如上所示的错误。我该如何解决?问题出在哪里?谢谢。
ColorDrawable cd = (ColorDrawable)dragged.getBackground();
int colorCode = cd.getColor();
v.setBackgroundColor(colorCode);
我有一个代码片段要在下面显示,我正在尝试更改视图的(v)背景。我将从TextView
(拖动的)获取颜色代码并View
使用此代码更改(v)的背景。但我收到如上所示的错误。我该如何解决?问题出在哪里?谢谢。
ColorDrawable cd = (ColorDrawable)dragged.getBackground();
int colorCode = cd.getColor();
v.setBackgroundColor(colorCode);
如果您只想将一个视图的背景分配给另一个视图而不仅仅是颜色,您可以使用以下代码
Drawable drawable = dragged.getBackground();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(drawable );
}else{
v.setBackground(drawable);
}
但是,如果您只想获取颜色,则必须提前确定分配给视图的可绘制对象类型。然后使用 instanceof 来处理如何获取相应可绘制对象的背景颜色。