这点小技巧,可以节省50%开发时间,却很容易被忽略

640?wx_fmt=gif&wxfrom=5&wx_lazy=1

分享开发中一点小知识点,关于TextView设置图标,类似下面的UI。

640?wx_fmt=png&wxfrom=5&wx_lazy=1

 

看着类似这样的UI,如果我们用ImageView 和TextView 组合来设计,那就有点小题大做了,其实一个TextView就足够了。

 

xml中的TextView:

    <TextView
   android:id="@+id/tv_about"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:drawableLeft="@mipmap/ic_launcher"
   android:text="关于我们" />


我们在xml中设置了 DrawableLeft。

那要是我想在代码中改变drawable。有什么方法可以使用代码为TextView设置drawableLeft呢?

答案是有的。

 

Drawable drawable= ContextCompat.getDrawable(this,
R.mipmap.ic_launcher);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
textView.setCompoundDrawables(drawable,null,null,null);



 这里要注意的是,需要设置setBounds,不然不会正常显示。

对此,我们还可以通过setCompoundDrawablesWithIntrinsicBounds这样设置.

 

textView.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);


具体可以参考

public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) {    }

[END]


希望今天的文章对你有帮助,感谢阅读,感谢有你

640?wx_fmt=jpeg


阅读推荐:


从事程序员的8大好处,怪不得很多女孩子都喜欢嫁给程序员


这个知识点,80%的求职者很容易陷入面试官精心设计的陷阱

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值