在Android button上添加icon,让icon和文字都居中显示
2013-09-29 18:30 王妞 阅读(4471) 评论(0) 编辑 收藏 举报参考这个链接:
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;
}
调用的时候: