简体   繁体   English

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

[英]How to set background color and underline for an ImageSpan

图片中的编辑文字

As shown above, I have inserted an image into a EditText , with text before and after it. 如上所示,我已将图像插入到EditText ,其前后带有文本。 So, I used an ImageSpan . 因此,我使用了ImageSpan

I wish to change the background color and underline the text dynamically, but I find that it doesn't make effect on the image. 我希望更改背景颜色并动态给文本加下划线,但是我发现它对图像没有影响。

What shall I do to make the ImageSpan show same effect with near text? 我该怎么做才能使ImageSpan在附近文本上显示相同的效果?

I have a partial solution to offer—for the background color. 对于背景色,我提供了部分解决方案。 The only way I found to do this (other than writing my own custom span class) is to put the image drawable into a LayerList atop a GradientDrawable that is your background color. 我发现做到这一点的唯一方法(除了编写自己的自定义span类)是将可绘制图像放入LayerList顶部的LayerList ,该GradientDrawable是背景色。 In code, it could be something like this: 在代码中,可能是这样的:

// 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);

You might possibly be able to use the same trick to simulate underlining (by adding another drawable to the layer list). 您可能可以使用相同的技巧来模拟下划线(通过向图层列表中添加另一个可绘制对象)。 However, I haven't experimented with this and can't offer a concrete suggestion. 但是,我尚未对此进行试验,因此无法提供具体建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM