setleft android,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参数赋值就可以了。对于不单独设置宽高还是很简便的。

两种情况,各取所需。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值