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