Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android setCompoundDrawables仅在drawable设置为其他imageview时工作?_Android - Fatal编程技术网

Android setCompoundDrawables仅在drawable设置为其他imageview时工作?

Android setCompoundDrawables仅在drawable设置为其他imageview时工作?,android,Android,我正在改变一个可绘制的颜色,然后将其设置为textview的可绘制左侧,但我观察到一件奇怪的事情。 只有在将可绘制设置为其他图像视图,然后再将其设置为文本视图时,可绘制左侧才起作用 Drawable mDrawable = this.getResources().getDrawable(R.drawable.legendc); mDrawable.setColorFilter(colorsActive[0], PorterDuff.Mode.SRC_I

我正在改变一个可绘制的颜色,然后将其设置为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不起作用,并且未应用任何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);