无法解析方法setCompoundDrawablesWithIntrinsicBounds

时间:2016-05-17 09:40:44

标签: android textview drawable

我正在尝试使用Strings设置图像资源

设置setCompoundDrawablesWithIntrinsicBounds有问题,它一直告诉我

cannot resolve method setCompoundDrawablesWithIntrinsicBounds( android.graphics.drawable.Drawable, 0, 0, 0);

我已经通过在线检查,即使在stackoverflow上,我的实现似乎也正确,但我无法编译。

String uri = "drawable/my_drawable_image";
int imageResource = getResources().getIdentifier(uri, null, getActivity().getPackageName());
Drawable image = getResources().getDrawable(imageResource);
myTextView.setCompoundDrawablesWithIntrinsicBounds(image, 0 ,0 , 0);

拜托,我做错了什么?

1 个答案:

答案 0 :(得分:1)

Drawable是一个对象。如果您使用setCompoundDrawablesWithIntrinsicBounds的重载版本,需要四个Drawable,则无法通过0未使用/默认值,但您必须使用空值。改变

myTextView.setCompoundDrawablesWithIntrinsicBounds(image, 0 ,0 , 0);

  myTextView.setCompoundDrawablesWithIntrinsicBounds(image, null , null , null);

setCompoundDrawablesWithIntrinsicBounds版没有Drawable作为第一个参数和三个int。有

setCompoundDrawablesWithIntrinsicBounds (int, int, int, int);也是。在这种情况下,您的代码将更改为

myTextView.setCompoundDrawablesWithIntrinsicBounds(imageResource, 0 ,0 , 0);