防止Android对话框缩进每个段落的文本

3

我相信之前已经有类似的问题,但我找不到它的任何信息...因此,除非有其他人知道链接,否则我将发布我的版本。

无论如何...

我有一个AlertDialog,它会在我的strings.xml文件中查找一个字符串来在对话框中显示。该字符串看起来类似于:

<string name="about_app">
        Blah blah blah talking about stuff this is a line
        Oh look I continued onto another line! Now I want these...\n\n

        So this is another paragraph! Intriguing. Yes, onto the next line 
        Android text doesn't necessarily consider this a new line, which is fine
        becauase i only care about paragraphs, which I'm using backslash n's for!\n\n

        And... here's my final statement! Woo.\n

</string>

在AlertDialog中,所有内容(基本上)都显示正确。 "Blah blah blah..." 与 AlertDialog 的左侧对齐,并继续直到遇到 \n\n。从这里开始,我得到了所需的间距,然后是"So this is..."。然而,"So this is..." 缩进了一个令人烦恼的小空格,我无法去掉!"And... here's my..."也是一样。有没有人有解决方法?注意:我尝试删除代码中每个段落后面的空行-没有任何作用。我还确保我的\n后面没有空格。

把 \n 放在段落前面怎么样 - 就像这样:`<string name="about_app"> 喋喋不休地谈论这些东西,这是一行 哦,看,我继续到另一行了!现在我想要这些... \n \n所以这是另一个段落!有趣。是的,进入下一行 Android 文本并不一定认为这是一个新行,这很好, 因为我只关心段落,我正在使用反斜杠 n 进行处理!\n \n还有...这是我的最后陈述!Woo。\n</string>` - Rajesh
之前评论中的格式不如我所预期。我的意思是在段落的第一个字符之前使用\n - Rajesh
1个回答

7
你看到的额外空格是由于在字符串资源中段落的缩进所导致。当你换行时,缩进作为单词之间的断点,你会将其视为空格。但是,当你有一个新的行时,缩进会显示在每个段落开始之前的空格上。要去除它,你需要直接在下一段的第一个字母之前放置\n或不缩进你的行。顺便说一句,独立的换行符也可能导致渲染时出现空格。这两个方法都可以:
<string name="about_app">
        Blah blah blah talking about stuff this is a line
        Oh look I continued onto another line! Now I want these...\n

        \nSo this is another paragraph! Intriguing. Yes, onto the next line 
        Android text doesn't necessarily consider this a new line, which is fine
        becauase i only care about paragraphs, which I'm using backslash n's for!\n

        \nAnd...here's my final statement! Woo.\n
</string>

或者(2)
<string name="about_app">
Blah blah blah talking about stuff this is a line
Oh look I continued onto another line! Now I want these...\n\n
So this is another paragraph! Intriguing. Yes, onto the next line 
Android text doesn't necessarily consider this a new line, which is fine
becauase i only care about paragraphs, which I'm using backslash n's for!\n\n
And...here's my final statement! Woo.\n
</string>

1
我使用了以下解决方案来去除额外的空格:String myString = getString(R.string.my_string).replace("\n ", "\n"); - sagis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接