http://gundumw100.iteye.com/blog/1205578
通常情况会想到drawableLeft,drawableTop,drawableRight,drawableBotton,这样是可以做,但只能在四个位置。
下面这种办法可以实现任意位置:
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.text.Html.ImageGetter; import android.widget.Button; public class demobutton extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageGetter imgGetter = new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable drawable = null; drawable = demobutton.this.getResources().getDrawable( Integer.parseInt(source)); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; } }; Button btn = (Button) findViewById(R.id.button1); StringBuffer sb = new StringBuffer(); sb.append("<img src=\"").append(R.drawable.button1).append("\"/>").append("<font color=\"6f6f6f\">按钮上加图片效果</font>");; Spanned span = Html.fromHtml(sb.toString(), imgGetter, null); btn.setText(span); sb = null; } }
采用Spanned也不是什么新鲜事了,呵呵。这里只提供一种思路。不止Button,任意可以显示文字的View都可以这样做。
时间: 2024-09-28 12:34:51