Android setCompoundDrawables仅在drawable设置为其他imageview时工作?
我正在改变一个可绘制的颜色,然后将其设置为textview的可绘制左侧,但我观察到一件奇怪的事情。 只有在将可绘制设置为其他图像视图,然后再将其设置为文本视图时,可绘制左侧才起作用Android setCompoundDrawables仅在drawable设置为其他imageview时工作?,android,Android,我正在改变一个可绘制的颜色,然后将其设置为textview的可绘制左侧,但我观察到一件奇怪的事情。 只有在将可绘制设置为其他图像视图,然后再将其设置为文本视图时,可绘制左侧才起作用 Drawable mDrawable = this.getResources().getDrawable(R.drawable.legendc); mDrawable.setColorFilter(colorsActive[0], PorterDuff.Mode.SRC_I
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不起作用,并且未应用任何DrawableLeft。
为什么会发生这种情况?为什么单独使用
setCompoundDrawables()
不起作用,可能与Android中的图像渲染和创建引用有关。每个Drawable
变量中都有一个名为mCallback
的参数。当您想跳过设置ImageView
时,它的值为空,否则它有一个WeakReference
变量-这意味着类似应用程序的内容会说“看,引用绑定到内存中的某个地方,现在我可以使用它了!”看起来像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);
setCompoundDrawables()
单独不起作用的原因可能与Android中的图像渲染和创建引用有关。每个Drawable
变量中都有一个名为mCallback
的参数。当您想跳过设置ImageView
时,它的值为空,否则它有一个WeakReference
变量-这意味着类似应用程序的内容会说“看,引用绑定到内存中的某个地方,现在我可以使用它了!”看起来像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);