Android and   in TextView

前端 未结 7 1967
一向
一向 2020-12-12 18:52

is it possible to add   in TextView? Has anyone achieved similar functionality?

I want to have non-breakable space in TextView.

相关标签:
7条回答
  • 2020-12-12 19:03

    This worked for me:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        textview.setText(Html.fromHtml(your string, Html.FROM_HTML_MODE_LEGACY));
    } else {
        textview.setText(Html.fromHtml(your string);
    }
    
    0 讨论(0)
  • 2020-12-12 19:16

    This is an example that using nbsp in a TextView

    <string name="text">Example:\u00A0</string>

    0 讨论(0)
  • 2020-12-12 19:18

    TextView respects the Unicode no-break space character (\u00A0), which would be a simpler/lighter solution than HTML.

    0 讨论(0)
  • 2020-12-12 19:18

    The TextView should respect the non breaking space

    <string name="test">Hello&#160;world</string>
    

    or

    new TextView("Hello\u00A0world");
    
    0 讨论(0)
  • 2020-12-12 19:20

    One unique situation I ran into was adding a non-breaking space to a string resource that took String.format parameters.

    <resources>
        <string name="answer_progress" formatted="false">Answered %d of %d</string>
    </resources>
    

    I tried to simply copy and past the non-breaking space character into the string and it was replaced with a regular old space after compiling.

    Removing the formatted="false", numbering the format args and using the backslash notation worked for me:

    <resources>
        <string name="answer_progress">Answered %1$d\u00A0of\u00A0%2$d</string>
    </resources>
    
    0 讨论(0)
  • 2020-12-12 19:22

    \u00A0 is a non-breaking space, \u0020 is not a non-breaking space

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