用代码为TextView设置drawable

warning: 这篇文章距离上次修改已过1751天,其中的内容可能已经有所变动。

用代码为TextView,Button 等设置drawableLeft,drawableRight,drawableTop,drawableBottom

以drawableLeft为例:

解决方案:

[codesyntax lang="java" container="none" tab_width="4"]

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) [/codesyntax]

参考网址:http://bbs.csdn.net/topics/390383463

最后修改于:2019年06月12日 14:31

已有 2 条评论

  1. 你好 我也是个站长 不知你对我的网站感不感兴趣 \r\n我的网站是\r\nhytp://tony1ee.com/\r\n欢迎常来啊

    1. 诸葛先生

      感谢支持,欢迎下次再来

添加新评论