TextView设置setCompoundDrawables不生效解决办法

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。

posted @ 2020-11-16 10:08  镇魂帆-张  阅读(1510)  评论(0编辑  收藏  举报