在 Android TextView 中,是否可以插入段落?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2979433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In an Android TextView, is it possible to insert paragraphs?
提问by JohnRock
Below is an example of the type of problem that I have. I have data in a pojo that I need to display in a textview... the data has pseudo code that denotes each paragraph with [p]
以下是我遇到的问题类型的示例。我在 pojo 中有数据,我需要在 textview 中显示......数据有伪代码,用 [p] 表示每个段落
I would like to somehow parse the [p]'s into paragraphs when they are displayed in the textview. Can this be done? Is there something I can substitute for the [p] that will make a new paragraph in the textview?
当 [p] 显示在文本视图中时,我想以某种方式将它们解析为段落。这能做到吗?有什么我可以代替 [p] 在 textview 中创建一个新段落的东西吗?
Question question = new Question();
question.setText("Here is the first paragraph.[p] And this should be the second.");
TextView view = (TextView) findViewById(R.id.qtext);
view.setText(question.getParsedText());
采纳答案by RoflcoptrException
Hi i would parse the whole String and then replace every [p] with \n or even \n\n as you like.
嗨,我会解析整个字符串,然后根据需要用 \n 甚至 \n\n 替换每个 [p]。
\n in the String makes a linebreak. For example use it like that:
字符串中的 \n 进行换行。例如像这样使用它:
question.setText("Here is the first paragraph.\n\n And this should be the second.");`
The method which can do that easily is String.replace(...)
可以轻松做到这一点的方法是String.replace(...)
回答by Santhosh Shettigar
You can also try this
你也可以试试这个
String summary = "<html><font color=\"#FFFFFF\"" +
<p align="+ "\"" +"left" + "\""+ ">" + "MY data" +"</p>"+
"</font></html>";
mTextView.setText(Html.fromHtml(summary));
回答by pankaj sharma
TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");