美文网首页
TextView.setCompoundDrawables()不

TextView.setCompoundDrawables()不

作者: 萧清轩 | 来源:发表于2019-02-21 12:01 被阅读0次

当我们在代码中设置TextView的Drawable时,会遇到Drawable不显示的问题,原因的因为,我们传入的Drawable没有调用setBounds();

drawable.setBounds(Rect bounds);

然而这种办法,写起来代码量和代码质量都不尽然,有没有其他方法?

setCompoundDrawablesWithIntrinsicBounds(Drawable start,Drawable top,Drawable end, Drawable bottom)

setCompoundDrawablesWithIntrinsicBounds(int start,int top, int end, int bottom)

这两种方法都为我们省去了调用 drawable.setBounds();
一个,是分别传入上下左右的Drawable,不设置的方向传null;
一个,是分别传入上下左右的资源id,不设置的方向传0;
例如

setCompoundDrawablesWithIntrinsicBounds(leftDrawable,null,null,null)
setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon,0,0,0)

相关文章

网友评论

      本文标题:TextView.setCompoundDrawables()不

      本文链接:https://www.haomeiwen.com/subject/fakryqtx.html