How to put space character into a string name in X

2019-01-21 01:40发布

i have defined some strings in the strings.xml file. Now I need to put some extra space between some numbers in the string. When I type extra space characters this is not showing on the application though.

Before:

<string name="spelatonertext3">-4, 5, -5, 6, -6,

And if I put extra space like this:

<string name="spelatonertext3">-4,  5, -5,   6,  -6,

It just looks the same on the app. How can I make space characters into the XML string?

14条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-21 01:50

You can use following as well

<string name="spelatonertext3"> "-4,  5, -5,   6,  -6, "> </string>

Put anything in " "(quotation) with space, and it should work

查看更多
家丑人穷心不美
3楼-- · 2019-01-21 01:51

to use white space in xml as string use &#160;. XML won't take white space as it is. it will trim the white space before setting it. So use &#160; instead of single white space

查看更多
Explosion°爆炸
4楼-- · 2019-01-21 01:52

If the output is HTML, then in HTML multiple spaces display as a single space. To prevent this, use non-breaking spaces (xA0) instead of ordinary spaces.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-21 01:53

As per your question if you want add spaces more than one in string resources their are many option to add spaces between character or word :

1.By default one space you can add directly in string resource file it working fine. but if give more than one space inside string resources file then it exclude that spaces. eg . -4, 5, -5, 6, -6,

  1. If you want add more extra spaces inside string resource file then uses:- i. adding unicode after character like

        <string name="test">-4,&#160;&#160;5,&#160;&#160;-5,&#160;&#160;6,&#160;&#160;-6,</string>
    

ii.you can use "\u0020"

<string name="test">-4,\u0020\u0020 5,\u0020\u00205 -5,\u0020\u00205 6,\u0020\u00205 -6,</string>
查看更多
Fickle 薄情
6楼-- · 2019-01-21 01:54

xml:space="preserve"

Works like a charm.

Edit: Wrong. Actually, it only works when the content is comprised of white spaces only.

Link

查看更多
甜甜的少女心
7楼-- · 2019-01-21 01:57

As already mentioned the correct way to have a space in an XML file is by using \u0020 which is the unicode character for a space.

Example:

<string name="spelatonertext3">-4,\u00205,\u0020-5,\u00206,\u0020-6</string>

Other suggestions have said to use &#160; or &#032; but there is two downsides to this. The first downside is that these are ASCII characters so you are relying on something like a TextView to parse them. The second downside is that &#160; can sometimes cause strange wrapping in TextViews.

查看更多
登录 后发表回答