LeadingMarginSpan2 - how to limit the number of paragraphs with margin

不问归期 提交于 2019-12-05 23:27:54

问题


I use a basic and very common implementation of a LeadingMarginSpan2 to wrap text around an image (since it seems to be the easiest way and please don't suggest me to use WebViews at this point):

  public class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 {

  private int margin;
  private int lines;

  public MyLeadingMarginSpan2(int lines, int margin) {
    this.margin = margin;
    this.lines = lines;
  }


  @Override
  public int getLeadingMargin(boolean first) {
    return first ? margin : 0; // <--- the issue is here
  }


  @Override
  public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
                              int top, int baseline, int bottom, CharSequence text,
                              int start, int end, boolean first, Layout layout) {
  }


  @Override
  public int getLeadingMarginLineCount() {
    return lines;
  }
 }

The problem is that as soon as a paragraph occurs in the text, an unwanted margin is given to this line. I want to limit the number of times getLeadingMargin() returns the actual margin to the number of lines passed inside the constructor.

I've tried to count how many times this margin was returned and compare it against the number of lines, this didn't work however (in most cases no margin was applied, in some cases it was applied to a wrong number of lines).

Does anyone have a workaround for this issue?

来源:https://stackoverflow.com/questions/27334638/leadingmarginspan2-how-to-limit-the-number-of-paragraphs-with-margin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!