如何为ImageSpan设置背景颜色和下划线

时间:2013-05-16 08:44:27

标签: java android image html

  

Image in edit text

如上所示,我已将图像插入EditText,并在其前后加上文字。所以,我使用了ImageSpan

我希望更改背景颜色并动态地为文本加下划线,但我发现它不会对图像产生影响。

如何使ImageSpan显示与近文相同的效果?

1 个答案:

答案 0 :(得分:0)

我有部分解决方案提供 - 背景颜色。我发现这样做的唯一方法(除了编写我自己的自定义跨度类)是将图像绘制到LayerList顶部GradientDrawable上,这是你的背景颜色。在代码中,它可能是这样的:

// Assume that d is the image drawable and bgSpan is the background color span.
// Then instead of doing
//      ImageSpan imgSpan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
// you do the following:

GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(bgSpan.getBackgroundColor());
LayerDrawable ld = new LayerDrawable(new Drawable[] {gd, d});
ld.setBounds(d.getBounds());
ImageSpan imgSpan = new ImageSpan(ld, ImageSpan.ALIGN_BASELINE);

您可能可以使用相同的技巧来模拟下划线(通过向图层列表添加另一个drawable)。但是,我没有对此进行过实验,也无法提供具体的建议。

相关问题