用代码为TextView设置drawable

用代码为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