android TextView的setCompoundDrawables()方法

这个方法可以在TextView的四周加上一个Drawable图标。对于只知道TextView显示文字的,是不是很高大上。

 setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

看代码就知道,参数顺序是左上右下。不想在四周加图片的话,可以设置为null。
  But,设置的Drawable有要求,必须要设置它的宽高,也就是要调用setBounds(int left, int top, int right, int bottom);其中需要给right和bottom设置固有宽度和固有高度,如果想设置drawable的宽高可以设置getIntrinsicWidth()和getIntrinsicHeight();

getIntrinsicWidth() /getIntrinsicHeight 获取view的固定宽度和高度(单位是dp)

setCompoundDrawablesWithIntrinsicBounds( Drawable left, Drawable  top, Drawable right, Drawable bottom)

这个方法比上边的就简便多了,等于将setBounds()这个方法合并到了setCompoundDrawablesWithIntrinsicBounds()的里边,直接将drawable的宽高自动获取。我们只需要将drawable参数赋值就可以了。对于不单独设置宽高还是很简便的。

两种情况,各取所需。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容