Using span to insert tab in textview

浪尽此生 提交于 2019-12-04 11:44:40

Just add "\t" for a tab, "\n" for a new line. For example:

String source = "Hello!\tLets select some text!";

Yes. combination of "\t" and TabStopSpannable will do the trick. The snippet below adds a tab between firstString and 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);

Be careful to use a meaningful value in the TabStopSpan.Standard constructor, preferably based on screendimensions.

use this.

public static SpannableStringBuilder getTabText(String text, float stop)
{
    int column = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, stop, SystemMaster.getResources().getDisplayMetrics());

    SpannableStringBuilder span = new SpannableStringBuilder(text);
    span.setSpan(new TabStopSpan.Standard(column), 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;
}

and then

Text text = "xx \t yy \n aa \t bb";
SpannableStringBuilder sp = TextHelper.getTabText(text, 100);
tv.setText(sp);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!