前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用代码为textview设置drawableLeft

使用代码为textview设置drawableLeft

作者头像
欢醉
发布2018-01-22 15:38:49
8180
发布2018-01-22 15:38:49
举报
文章被收录于专栏:james大数据架构james大数据架构

xml中的textView中设置android:drawableLeft:

代码语言:js
复制
<TextView         
android:id="@+id/bookTitle" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:drawableLeft="@drawable/checkmark" 
android:gravity="center_vertical" 
android:textStyle="bold" 
android:textSize="24dip" 
android:maxLines="1" 
android:ellipsize="end"/>  

如程序中所见我在xml中设置了 DrawableLeft。 我想在代码中改变drawable。 有什么方法可以使用代码为textview设置drawableLeft呢?

解决方案

代码语言:js
复制
public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);  

类似调用方法如下:

1.在XML中使用

代码语言:js
复制
android:drawableLeft="@drawable/icon" 

2.代码中动态变化

代码语言:js
复制
Drawable drawable= getResources().getDrawable(R.drawable.drawable);  
/// 这一步必须要做,否则不会显示. 
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
myTextview.setCompoundDrawables(drawable,null,null,null);  

也或参考另一个函数

代码语言:js
复制
 public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,  Drawable top, Drawable right, Drawable bottom) 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-05-18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档