android textview Html的 图片居中


   
  Spanned tex = Html.fromHtml("我们<img src='" + R.drawable.ic_launcher + "'/>这是测试", imageGetter,
  
new MyTagHandler(getApplicationContext()));
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText(tex);
tv.setTextColor(Color.RED);

我textview加载html,在html中带有图片的
现在实现这种:
现在实现这种

但是如何实现文字图片居中
如图:
请输入图片描述

事例代码

   
  public class aaaas extends Activity {
  
private EditText editText1;
private Button button1;
TextView tv, tv2, tv4;
QuestionTextView tv3;
Canvas canvas = null;
SpannableString spanText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ttes);
tv = (TextView) findViewById(R.id.textView1);

Spanned tex = Html.fromHtml("我们<img src='" + R.drawable.ic_launcher + "'/>这是测试", imageGetter,
new MyTagHandler(getApplicationContext()));
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText(tex);
tv.setTextColor(Color.RED);

}

Drawable drawable;
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
int id = Integer.parseInt(source);
String icon = "iVBORw0KGgoAAAANSUhEUgAAAF8AAABLCAYAAAAWC1t4AAAHoUlEQVR4nO2ccWwbVx3Hv7/zORkwBO4UISfSOhV1GpNgwDQq1tEyOkoFG6xFXoXK1Fr2/S5x1m6dKOtWxg7QVk0FsbTE9d05a4GhaZRpYkJaVU2qBB0QoQkhCkLaJAI0tqZREchANL67H3/kbLmZ7Ti2kyvt+/zzTne/9/zy8bvfPb+7C6CIDD3qDlzJKPkRouRHiJLfQwzDuF5E/lksFt9oJ17J7xHMvBPAGIBRAD9qp46S3wGpVKpv1apVdwB4dxAE64nocwDWhIe1dttR8jtgYGCgz/f9bBAEswAmdV0/5HneawD6l9KOkt8B+Xz+LQDb6vcx85LbUfIjRMmPECU/QpT8CFHyI0TJjxAlP0KU/AhR8iNEyY8QJT9ClPwIUfIjRMmPECU/QpT8LrEsSyuVSp9HeCOFiL6we/funx45cuRfi9VV8juE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U/Aj5H7sLH4vqOpikAAAANWlUWHRjb3B5d3JpZ2h0AAAAY29weXdyaWdodAAAQmVpamluZyBGZW5iaSBXZWlMYWkgQ28uIEx0ZNf0atsAAAAASUVORK5CYII=";
byte[] img = Base64.decode(icon.getBytes(), Base64.DEFAULT);
Bitmap bitmap;
if (img != null) {
bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);

drawable = new BitmapDrawable(bitmap);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
}

return drawable;

// Drawable d = getResources().getDrawable(id);
// d.setBounds(0, 0, d.getIntrinsicWidth(),d.getIntrinsicHeight());
// return d;

}

};
}

AndroidUI Android

chiseen 10 years, 4 months ago

Your Answer