在Android TextView中,是否可以插入段落?

时间:2010-06-05 06:34:38

标签: android textview

以下是我遇到的问题类型的示例。我在pojo中有数据需要在textview中显示...数据有伪代码,用[p]

表示每个段落

我想以某种方式将[p]解析为在textview中显示的段落。可以这样做吗?有什么东西可以替代[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());

4 个答案:

答案 0 :(得分:10)

嗨我会解析整个字符串,然后根据需要用\ n或甚至\ n \ n替换每个[p]。

String中的

\ n生成一个换行符。例如,使用它:

question.setText("Here is the first paragraph.\n\n And this should be the second.");`

可以轻松完成此操作的方法是String.replace(...)

答案 1 :(得分:5)

你也可以尝试这个

String summary = "<html><font color=\"#FFFFFF\"" +

             <p align="+ "\"" +"left" + "\""+ ">" +  "MY data" +"</p>"+

                "</font></html>";
mTextView.setText(Html.fromHtml(summary));

答案 2 :(得分:1)

TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");

答案 3 :(得分:0)

http.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class); http .csrf() .requireCsrfProtectionMatcher(csrfRequestMatcher) .and() .authorizeRequests() .antMatchers("/**/**") .permitAll() .anyRequest().authenticated() .and() .formLogin() .successHandler(authenticationSuccessHandler) .and() .logout() .logoutSuccessHandler(logoutSuccessHandler) .permitAll(); http.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class); <br>如果您需要其他格式(如粗体)且位于Html.fromHtml的中间位置,则可以正常工作:

<p>

在Android 22上测试。