使用代码为textview设置drawableLeft

问答小助手 2013-03-06 10:54:06
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/903

原问题描述:

xml中的textView:
<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呢?

解决方案:
public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);


类似调用方法如下:

1.在XML中使用
android:drawableLeft="@drawable/icon"



2.代码中动态变化
Drawable drawable= getResources().getDrawable(R.drawable.drawable);
/// 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
myTextview.setCompoundDrawables(drawable,null,null,null);


也或参考另一个函数

public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,
Drawable top, Drawable right, Drawable bottom)
...全文
35947 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
thresh0ld 2015-08-31
  • 打赏
  • 举报
回复
膜拜,感谢。。。一直找不到原因为什么不显示
AKA.Dennis.Li 2014-09-04
  • 打赏
  • 举报
回复
确实有很大的帮助,谢谢
顺丨逐风者 2014-07-31
  • 打赏
  • 举报
回复
可以了 完美实现
猴哥说Java 2013-07-23
  • 打赏
  • 举报
回复
多谢了,搞半天没搞出来
bowen614 2013-04-19
  • 打赏
  • 举报
回复
厉害厉害!学习
thomas_xh001 2013-03-12
  • 打赏
  • 举报
回复
学习了
Ada168855 2013-03-12
  • 打赏
  • 举报
回复
TextView tv=new TextView(this); tv.setCompoundDrawables(left, top, right, bottom); 如果只是在TextView的left设置图片,那么其他设置为null
chuyu95 2013-03-12
  • 打赏
  • 举报
回复
看看再说吧!
sssistps5151 2013-03-12
  • 打赏
  • 举报
回复
看看再说了吧一
smz8_2004 2013-03-11
  • 打赏
  • 举报
回复
学习。。。。。
z18485901 2013-03-11
  • 打赏
  • 举报
回复
lhw7791086 2013-03-10
  • 打赏
  • 举报
回复
li787992250 2013-03-10
  • 打赏
  • 举报
回复
楼主好厉害A
eatcatfish 2013-03-07
  • 打赏
  • 举报
回复
xsq9000 2013-03-07
  • 打赏
  • 举报
回复
wangshunguo197911 2013-03-07
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
楼主好厉害
rpg55555 2013-03-06
  • 打赏
  • 举报
回复
学习。。。。。
Gt244239732 2013-03-06
  • 打赏
  • 举报
回复
真的很用心!正在找!
lineCode 2013-03-06
  • 打赏
  • 举报
回复

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧