打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

2022.08.02 广东

关注

看着类似这样的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]

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android 自定义RadioButton 实现文字上下左右方向的图片大小设置
Android中UI设计的一些技巧!!!
Android进阶:ListView性能优化异步加载图片 使滑动效果流畅
Android 中级教程之------Android MediaPlayer播放mp3的实...
在EditText中插入表情图片 (CharacterStyle&SpannableStr...
Android学习小Demo(22)带删除按钮的TextView
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服