我正在更改可绘制的颜色,然后将其设置为TextView的可绘制左侧,但是我观察到了一件奇怪的事情。可绘制的左图仅在将可绘制的一些图像视图设置为文本视图之前将其设置为其他图像视图。
Drawable mDrawable = this.getResources().getDrawable(R.drawable.legendc);
mDrawable.setColorFilter(colorsActive[0], PorterDuff.Mode.SRC_IN);
mImageview.setImageDrawable(mDrawable);
mtextview.setCompoundDrawables(mDrawable, null, null, null);
如果我删除mimageview.setimagedrawable(mdrawable(;然后,setCompoundDrawables不起作用,并且没有可绘制的剩余。为什么会发生?
单独使用setCompoundDrawables()
不起作用的原因可能与在Android中的图像渲染和创建参考有关。每个Drawable
变量中都有一个称为mCallback
的参数。当您要跳过设置ImageView
时,它的值为null,否则它具有WeakReference
变量 - 这意味着诸如App之类的内容会说"看,引用,参考已绑定到内存中的某个地方,现在我可以使用它!" 看起来setImageDrawable()
方法创建了这种绑定,而setCompoundDrawables()
则没有。
我不是这个主题的专家,我发现只是解决方法(也许您需要一个类似ImageLoader
的对象来处理此问题(,但看起来使用mtextview.setCompoundDrawablesWithIntrinsicBounds()
效果很好。
//mImageview.setImageDrawable(mDrawable); You can delete this line
//Using this will not require to load your Drawable somewhere else
mtextview.setCompoundDrawablesWithIntrinsicBounds(mDrawable, null, null, null);