您的位置:首页 > 其它

TextView的setCompoundDrawables不显示问题解决

2015-08-24 21:00 561 查看

TextView的setCompoundDrawables不显示问题解决

[align=center] [/align]

[align=center] [/align]

TextView可以用过调用setCompoundDrawables设置一张图片出现在上下左右四个地方。代码如下:

Drawable drawable = getResources().getDrawable(R.drawable.img);
drawable.setBounds(0, 0, 32, 32);
textView.setCompoundDrawables(drawable, null, null, null);

注意到这个Drawables必须已经调用过了setBounds。如果不设置则无法显示。

项目中的代码:

public static void setTextDrawableRight(Context context, TextView tv, int id) {
Drawable drawable = context.getResources().getDrawable(id);
if (drawable != null) {
drawable.setBounds(0, 0, Units.dip2px(12f), Units.dip2px(6f));
}
tv.setCompoundDrawables(null, null, drawable, null);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: