gpt4 book ai didi

android - 使用 span 在 textview 中插入标签

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:34 26 4
gpt4 key购买 nike

有没有办法在 textview 中使用 span 将制表符添加到一段文本?从开发人员文档中,我遇到了 TabStopSpan ( see here ) 并尝试在 TextView 中使用它,如下所示:

String source = "Hello World! Let's select some text!!";

SpannableString s = new SpannableString(source);
s.setSpan(new TabStopSpan() {
@Override
public int getTabStop() {
return 100;
}
}, 5, 10, 0);
ed.setText(s, BufferType.SPANNABLE);

其中 ed 是一个 TextView。上面的代码 fragment 没有做任何事情,文档也少得几乎没有用。有什么方法可以使用 span 将制表符添加到 TextView 吗?

最佳答案

是的。 "\t"和 TabStopSpannable 的组合就可以了。下面的代码 fragment 在 firstString 和 secondString 之间添加了一个制表符:

    SpannableStringBuilder span = new SpannableStringBuilder(firstString +"\t"+ secondString);
span.setSpan(new TabStopSpan.Standard(600), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
}

((TextView) v).setText(span, TextView.BufferType.SPANNABLE);

在 TabStopSpan.Standard 构造函数中小心使用有意义的值,最好基于屏幕尺寸。

关于android - 使用 span 在 textview 中插入标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19121125/

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