Line Break in XML formatting?

后端 未结 5 990
夕颜
夕颜 2021-01-31 06:56

when editing a String in XML I need to add line breaks. And I wanted to ask what is the RIGHT form when programming for android? Because
works but ECLIPS

相关标签:
5条回答
  • 2021-01-31 07:27

    Here is what I use when I don't have access to the source string, e.g. for downloaded HTML:

    // replace newlines with <br>
    public static String replaceNewlinesWithBreaks(String source) {
        return source != null ? source.replaceAll("(?:\n|\r\n)","<br>") : "";
    }
    

    For XML you should probably edit that to replace with <br/> instead.

    Example of its use in a function (additional calls removed for clarity):

    // remove HTML tags but preserve supported HTML text styling (if there is any)
    public static CharSequence getStyledTextFromHtml(String source) {
        return android.text.Html.fromHtml(replaceNewlinesWithBreaks(source));
    }
    

    ...and a further example:

    textView.setText(getStyledTextFromHtml(someString));
    
    0 讨论(0)
  • 2021-01-31 07:31

    If you are refering to res strings, use CDATA with \n.

    <string name="about">
        <![CDATA[
     Author: Sergio Abreu\n
     http://sites.sitesbr.net
      ]]>        
    </string>
    
    0 讨论(0)
  • 2021-01-31 07:35

    Also you can add <br> instead of \n.

    And then you can add text to TexView:

    articleTextView.setText(Html.fromHtml(textForTextView));
    
    0 讨论(0)
  • 2021-01-31 07:44

    Use \n for a line break and \t if you want to insert a tab.

    You can also use some XML tags for basic formatting: <b> for bold text, <i> for italics, and <u> for underlined text.

    Other formatting options are shown in this article on the Android Developers' site:
    https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

    0 讨论(0)
  • 2021-01-31 07:50

    Take note: I have seen other posts that say &#xA; will give you a paragraph break, which oddly enough works in the Android xml String.xml file, but will NOT show up in a device when testing (no breaks at all show up). Therefore, the \n shows up on both.

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