TextView可以用过调用setCompoundDrawables设置一张图片出现在上下左右四个地方。代码如下:
Drawable drawable = getResources().getDrawable(R.drawable.img);
drawable.setBounds(0, 0, 32, 32);
textView.setCompoundDrawables(drawable, null, null, null);
注意到这个Drawables必须已经调用过了setBounds。如果不设置则无法显示。
转自:http://www.amsoft.cn/post-108.html
--------------------------------------------------------------------
TextView设置setCompoundDrawables不生效解决,原因是指定drawable的大小,
解决方案:
TextView tvFruit = view.findViewById(R.id.fruit_name);
tvFruit.setText(fruit.getName());
Drawable dra = context.getDrawable(fruit.getImageId()) ;
dra.setBounds(0, 0, dra.getIntrinsicWidth(), dra.getIntrinsicHeight());;
tvFruit.setCompoundDrawables(dra,null,null,null);
setBounds四个参数的意思:
x - 组件的新 x 坐标。
y - 组件的新 y 坐标。
width - 组件的新 width,单位px。
height - 组件的新 heigh,单位px。