本文介绍了ReplacementSpan在multiautocomplete的TextView重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有空间标记生成器多个自动填充文本查看和使用ReplacementSpan在每个联系人

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

我的自定义更换code是

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

我在这里设置spannable字符串

i set spannable string here

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

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

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

请看到的屏幕

1,单行

2.multiline图片

2.multiline image

当我使用这样的y值

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

推荐答案

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

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

因为你没有使用你的DrawText方法的y值出现的重叠问题。如果用它替换为

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

看起来应该适当

这篇关于ReplacementSpan在multiautocomplete的TextView重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:58