How to increase the spacing between paragraphs in a textview

前端 未结 5 2072
余生分开走
余生分开走 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条回答
  • 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.

    0 讨论(0)
  • 2021-02-06 22:31

    No need to manipulate spacing between paragraphs.

    FROM_HTML_MODE_LEGACY: Separate block-level elements with blank lines (two newline characters) in between. This is the legacy behavior prior to N.

    textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
        } else {
            Html.fromHtml(html)
        }
    
    0 讨论(0)
  • 2021-02-06 22:34

    You can use

    Html.fromHtml(String);
    

    This will help you to write string in form of html where you can use the html tags like <p>, <h1> etc

    Eg:

    myTextView.setText(Html.fromHtml("<p>This is it first<br>paragraph.</p><p>This is the second<br>paragraph.</p>"));
    
    0 讨论(0)
  • 2021-02-06 22:39

    I have faced a similar problem. Finally, I fixed with spanable text for newline character. Here is the code goes.

        String text ="Lorem ipsum dolor sit amet, End of first line.\nBegining of the second line, Aenean justo mi, faucibus eu suscipit id, finibus gravida quam. End of Second Line.\nBegining of the third line, Aliquam ac diam tempus, pharetra nibh at, imperdiet diam.  End of Third Line.\n Begining of the fourth line, Fusce placerat erat dolor, id tristique lacus gravida ac. End of Fourth Line.\nBegining of the Fifth line, Sed id molestie est, sed elementum lectus.  End of Fifth Line.\n";
    
        text = text.replace("\n","\n\0");
        Spannable spannable = new SpannableString(text);
        for (int i = 0; i < text.length()-1; i++) {
            if (text.charAt(i) == '\n') {
                spannable.setSpan(new RelativeSizeSpan(1.5f), i+1, i+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        mTextMessage.setText(spannable, TextView.BufferType.SPANNABLE);
    
    0 讨论(0)
  • 2021-02-06 22:41

    I'm afraid it's not adjustable in TextView. Here is the document of all the TextView attributes: https://developer.android.com/reference/android/R.styleable#TextView I guess the best way of doing this could be using one TextView for each paragraph, and then define margin between TextView.

    I think it's a similar design conception as p tag in html. Within the tag is a whole paragraph, then define the layout of p tag.

    0 讨论(0)
提交回复
热议问题