在Android button上添加icon,让icon和文字都居中显示

参考这个链接:

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;
 }

调用的时候:

exportBtn = (Button)findViewById(R.id.setting_export_btn);
   addButtonIcon(exportBtn, R.drawable.icon_export, R.string.setting_export);

 

转载于:https://www.cnblogs.com/iceface/p/3346193.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值