How to increase the spacing between paragraphs in a textview

前端 未结 5 2082
余生分开走
余生分开走 2021-02-06 21:54

I have some text that have more than one paragraph (using \"\\n\") and want to put a spacing between the paragraphs, but without using \"\\n\\n\". But the text from the same par

5条回答
  •  旧时难觅i
    2021-02-06 22:30

    You can use Spannable's to achieve this:

    String formattedText = text.replaceAll("\n", "\n\n");
    SpannableString spannableString = new SpannableString(formattedText);
    
    Matcher matcher = Pattern.compile("\n\n").matcher(formattedText);
    while (matcher.find()) {
        spannableString.setSpan(new AbsoluteSizeSpan(25, true), matcher.start() + 1, matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    

    The code above replaces all line breaks with two line breaks. After that it sets absolute size for each second line break.

提交回复
热议问题