参考这个链接:
http://gundumw100.iteye.com/blog/1205578
在xml文件里仍然有这个button,只不过不加icon和文字。
写了个函数:
private void addButtonIcon(Button btn, int icon_drawable, int text_id)
{
ImageGetter imgGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable = null;
drawable = getResources().getDrawable(
Integer.parseInt(source));
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
};
StringBuffer sb = new StringBuffer();
String text = " " + getString(text_id); //add two space to look better
sb.append("<img src=\"").append(icon_drawable).append("\"/>").append(text);
Spanned span = Html.fromHtml(sb.toString(), imgGetter, null);
btn.setText(span);
sb = null;
}
调用的时候: