如何在 Android TextView 中添加换行符? [英] How to add a line break in an Android TextView?

查看:28
本文介绍了如何在 Android TextView 中添加换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 TextView 中添加换行符.

I am trying to add a line break in the TextView.

我尝试了建议的 但这没有任何作用.这是我设置文本的方式.

I tried suggested but that does nothing. Here is how I set my texts.

TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));

这是我的字符串:<string name="sample_string">some test line 1 some test line 2</string>

This is my String: <string name="sample_string">some test line 1 some test line 2</string>

应该是这样的:

some test line 1
some test line 2

但它显示如下:some test line 1 some test line 2.

我错过了什么吗?

推荐答案

想通了:

<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>

因此需要在 CDATA 中进行包装,并在其中添加中断作为 html 标记

so wrap in CDATA is necessary and breaks added inside as html tags

这篇关于如何在 Android TextView 中添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆