ReplacementSpan in multiautocomplete textview overlap

我与影子孤独终老i 提交于 2019-12-11 09:27:33

问题


Hi i have multiple autocomplete text view with space tokenizer and use ReplacementSpan for background color change in each contacts

my custom replacement code is

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;
    }
}

i set spannable string here

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

its ok when multiple auto complete textview have single line but it come to multiple line means it overlap each text

please see the screens

1.single line

2.multiline image

When i use y value like this

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


回答1:


If you are using your ReplacementSpan only to change the background color you could as well simply use a BackgroundColorSpan:

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

The overlap problem occurs because you are not using the y value in your DrawText Method. If you replace it with

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

it should look proper



来源:https://stackoverflow.com/questions/30747417/replacementspan-in-multiautocomplete-textview-overlap

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