How to set background color and underline for ImageSpan

Image in edit text

As shown above, I pasted the image into the EditText with text before and after it. So, I used ImageSpan .

I want to change the background color and emphasize the text dynamically, but I found that it does not affect the image.

What should I do to make ImageSpan the same effect with close text?

+2
source share
1 answer

I have a partial solution suggesting a background color. The only way I did this (other than writing my own span class) is to display the image in a LayerList on top of the GradientDrawable , which is your background color. 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 may be able to use the same trick to simulate underlining (adding another picture to the list of layers). However, I have not experimented with this and cannot offer a concrete proposal.

0
source

All Articles