在文本视图中添加图像有效。将图像高度和宽度更改为文本视图文本高度不起作用。 (原始图像“红色圆圈”是:32px x 32px)
输出如下:
test img http://img850.imageshack.us/img850/9500/m1f.png
XML:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
代码:
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
String getText = getResources().getString(R.string.getText);
textView.setText(getText);
SpannableStringBuilder ssb = new SpannableStringBuilder(getText);
Bitmap redcircle= BitmapFactory.decodeResource( getResources(), R.drawable.redcircle );
Bitmap resizedbitmap = Bitmap.createScaledBitmap(redcircle, (int)textView.getMeasuredHeight(), (int)textView.getMeasuredHeight(), true);
ssb.setSpan( new ImageSpan( resizedbitmap ), 3, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
textView.setText( ssb, BufferType.SPANNABLE );
}
编辑:
我想,图像的高度和宽度等于文本视图文本高度。例如:
答案 0 :(得分:1)
尝试使用
ImageSpan(resizedbitmap, ImageSpan.ALIGN_BASELINE)
而不是
ImageSpan(resizedbitmap)
ImageSpan
的默认对齐方式为ALIGN_BOTTOM
。