ReplacementSpan在multiautocomplete的TextView重叠(Repla

2019-10-23 07:31发布

嗨,我有空间标记生成器自动完成多种文本浏览和使用ReplacementSpan在每个联系人的背景颜色变化

我的自定义更换代码

public class MyForgroudSpan : ReplacementSpan
{
    public override void Draw(Canvas canvas, ICharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint)
    {

        var rect = new RectF(x , top, x + paint.MeasureText(text, start, end)+8, bottom);
        paint.Color = Android.Graphics.Color.ParseColor("#E5E5E6");
        canvas.DrawRect(rect, paint);
        paint.Color = Android.Graphics.Color.Black;
        int xPos = Java.Lang.Math.Round(x + (8 / 2));
        int yPos = (int)((canvas.Height / 2) - ((paint.Descent() + paint.Ascent()) / 2));

        canvas.DrawText(text, start, end, xPos, yPos, paint);

    }
    public override int GetSize(Paint paint, ICharSequence text, int start, int end, Paint.FontMetricsInt fm)
    {
        return Java.Lang.Math.Round(paint.MeasureText(text, start, end))+8;
    }
}

我在这里设置spannable串

SpannableStringBuilder ssb = new SpannableStringBuilder(Text.trim());
ssb.SetSpan(new MyForgroudSpan(), x, x + c.Length, SpanTypes.ExclusiveExclusive);

其确定当多个自动完成的TextView有单线,但它来多线意味着它重叠文本

请参阅屏幕

1.单行

2.multiline图像

当我用这样的y值

canvas.DrawText(text, start, end, xPos, y, paint);

Answer 1:

如果您使用的是ReplacementSpan只有改变你还可简单地使用BackgroundColorSpan背景色:

ssb.SetSpan(
  new BackgroundColorSpan(
    Android.Graphics.Color.ParseColor("#E5E5E6"), 
    x,
    x + c.Length, 
    SpanTypes.ExclusiveExclusive));

因为你不使用你的DrawText方法y值出现的重叠问题。 如果你将其替换为

canvas.DrawText(text, start, end, xPos, y, paint);

它应该是正确的



文章来源: ReplacementSpan in multiautocomplete textview overlap