Insert line separator in spannablestringbuilder af

2019-08-08 20:51发布

问题:

I have a spannablestringbuilder with a number of imagespans in it. I would like to insert a line separator after the image to ensure that text following it starts on the next line.

I am trying to do this in a loop, but it doesn't insert the line seprator and breaks the spans.

ImageSpan[] imageSpans = strBuilder.getSpans(0, strBuilder.length(), ImageSpan.class);
    for (ImageSpan imageSpan : imageSpans) {
        strBuilder = strBuilder.insert(strBuilder.getSpanEnd(imageSpan), System.getProperty("line.separator") );
    }

Any ideas?

回答1:

My issue was that I was adding new ImageSpans elsewhere in the code and not removing previously added ones. This led to the inconsistencies, removing old imagespans after adding a new one resolved the issue.