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。