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。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM