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

3
如上图所示,我在一个EditText中插入了一张图片,并在其前后分别添加了文本。为此,我使用了一个ImageSpan
我希望能够动态更改背景颜色并给文本加下划线,但是我发现这对图片没有任何影响。
请问如何使ImageSpan的效果与相邻的文本相同?

在textchanged方法上使用TextWatcher - Harsh Dev Chandel
谢谢,但那不是我需要的...在所有文本输入后,我如何更改ImageSpan的样式? - Emmanuel
1个回答

0
我有一个部分解决方案可以提供——针对背景颜色。我发现除了编写自己的自定义 span 类之外,唯一的方法是将图像 drawable 放入 LayerList 中,覆盖在一个 GradientDrawable 上,该 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);

你可能可以使用相同的技巧来模拟下划线(通过向图层列表添加另一个可绘制对象)。然而,我没有尝试过这个方法,无法提供具体建议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接