我必须从我的资源/值文件连接这两个字符串:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2"> flips !</string>

我是这样做的:

String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2);

Toast.makeText(this, message_all_pairs_found, 1000).show();

而是在第一个弦的末尾和第二个弦的开头 (在播放祝酒词时)……

我该怎么办?

我想答案就在这个文档链接中

或者类似于使用&对于“&”字符??


当前回答

如果你真的想用你以前做的方式来做,那么我认为你必须通过转义来告诉它空白是相关的:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string>
<string name="Toast_Memory_GameWon_part2">\ flips !</string>

但是,我会使用字符串格式。大致如下:

<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>

then

String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);

其他回答

这里所有的答案都不适合我。相反,在XML字符串的末尾添加一个空格,我这样做了

<string name="more_store">more store<b> </b> </string>

我也遇到了同样的问题。我想在表示屏幕上字段名的资源字符串的末尾留下一个空白。

我在这个问题报告上找到了一个解决方案:https://github.com/iBotPeaches/Apktool/issues/124

这和Duessi提出的观点是一样的。直接在XML中插入\u0020作为您想保留的空白。

例子:

<string name="your_id">Score :\u0020</string>

替换是在构建时完成的,因此它不会影响游戏的性能。

还有使用CDATA的解决方案。例子:

<string name="test"><![CDATA[Hello          world]]></string>

但总的来说,我认为\u0020已经足够好了。

如果你真的想用你以前做的方式来做,那么我认为你必须通过转义来告诉它空白是相关的:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string>
<string name="Toast_Memory_GameWon_part2">\ flips !</string>

但是,我会使用字符串格式。大致如下:

<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>

then

String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);

我对Android没有特别的想法,但这看起来像通常的XML空白处理-元素中的前导和尾随空白通常被认为是无关紧要的并被删除。尝试xml:空间:

<string name="Toast_Memory_GameWon_part1" xml:space="preserve">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2" xml:space="preserve"> flips !</string>