gpt4 book ai didi

android - 如何在多行文本的行尾添加 ImageSpan

转载 作者:行者123 更新时间:2023-11-29 01:17:19 45 4
gpt4 key购买 nike

我想在多行字符串的每一行末尾添加一个跨度。我试着只画一个字符,但行尾什么也没有出现。但是当设置了ImageSpan时,可以增加行高,但行内仍然没有图像。

private void addSpansAtEnd() {
String s = "First line\nSecond line\Third line\n";
SpannableStringBuilder e = new SpannableStringBuilder(s);
int stop = start + count;
for (int start = -1;
(start = s.indexOf("\n", start)) > -1 && start < stop;
++start) {
e.setSpan(
/*new ImageSpan(getContext(), R.drawable.ic_line),*/
new NewLineSpan(),
start,
start + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}

/* Trying to replace \\n with other character. */
private class NewLineSpan extends ReplacementSpan {
@Override
public int getSize(
Paint paint,
CharSequence text,
int start,
int end,
Paint.FontMetricsInt fm) {
return 0;
}

@Override
public void draw(
Canvas canvas,
CharSequence text,
int start,
int end,
float x,
int top,
int y,
int bottom,
Paint paint) {
paint.setColor(0xFF666666);
canvas.drawText("o", x, y, paint);
}
}

如何在行尾绘制?

最佳答案

将跨度放在换行符上似乎有点奇怪。换行符跨越两行但没有任何宽度。我认为如果在换行符之前添加一个空格然后将 span 放在空格周围,你可能会更幸运:

for (int start = -1;
(start = s.indexOf("\n", start)) > -1 && start < stop;
start += 2) { // increment by 2 to skip the displaced newline
e.insert(start, " "); // insert a space for the span
e.setSpan(new ImageSpan(getContext(), R.drawable.ic_line),
start,
start + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
stop++; // because a space was added
}

关于android - 如何在多行文本的行尾添加 ImageSpan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38792253/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com