以下是我遇到的问题类型的示例。我有一个pojo中的数据,我需要在textview中显示...数据有伪代码,用[p]表示每个段落
我想以某种方式将[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());
嗨,我将解析整个字符串,然后用\ n或甚至\ n \ n替换每个[p]。
\ n在String中生成一个换行符。例如,使用它:
question.setText("Here is the first paragraph.\n\n And this should be the second.");`
可以轻松做到的方法是 与string.replace(...)
你也可以试试这个
String summary = "<html><font color=\"#FFFFFF\"" +
<p align="+ "\"" +"left" + "\""+ ">" + "MY data" +"</p>"+
"</font></html>";
mTextView.setText(Html.fromHtml(summary));
TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");
<br>
和 <p>
如果你需要其他格式,如粗体,并在中间的工作 Html.fromHtml
:
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView tv = new TextView(this);
tv.setText(Html.fromHtml("first<br><b>second</b>"));
setContentView(tv);
}
}
在Android 22上测试过。