Android 断行而不增加文本长度

Android 断行而不增加文本长度,android,textview,Android,Textview,我可以使用以下代码断开一行: String str1 = "TEST1"; // length = 5 String str2 = "TEST2"; // length = 5 TextView textView = (TextView)findViewById( R.id.text_view ); textView.setText(str1 + '\n' + str2); 但最终文本长度等于11 问题: 是否有任何特殊字符或方法允许我在TextView中获得相同的结果,而不增加文本长度

我可以使用以下代码断开一行:

String str1 = "TEST1"; // length = 5
String str2 = "TEST2"; // length = 5

TextView textView = (TextView)findViewById( R.id.text_view );

textView.setText(str1 + '\n' + str2);
但最终文本长度等于11

问题:

是否有任何特殊字符或方法允许我在
TextView
中获得相同的结果,而不增加文本长度

我正在努力实现的目标:

我有一个数据格式,它存储在JSON中。看起来像

[{type: line, params: {line params}}, {type: text, params: {text params}, ...]
  • 开头总是有一行

  • 每个段落都以行开头(因此它就像一个行分隔符,存储在行首,而不是行尾)

  • 每行的大小等于1,即每行计为一个字符

  • 每个段落以文本的最后一个字符结尾(不是“\n”)

  • 有一些行参数(如BulletList、数字列表、段落)


我需要在我的
TextView
和源数据之间进行严格的映射,也就是说,对于我的
TextView
中的每个光标位置,我需要计算在源数据中有多少个字符在它前面。

对于您的问题,我的答案是否定的。但是您可以创建自己的
TextView
,并更改它计算文本长度的方式,例如忽略“/n”当计算长度时。

嗯,有一个棘手的方法

    String str1 = "TEST1"; // length = 5
        String str2 = "TEST2"; // length = 5

        textView = (TextView)findViewById( R.id.textView1 );
        textView.setWidth(120);
        textView.setTextSize(20);

        textView.setText(str1  + str2);

//textView.getText().toString().length()  length = 10
在XMl中

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

取两个文本视图,然后在另一个文本视图下添加一个。这样您就不会发现任何长度问题

 like :   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView1" />
<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:text="TextView2" />
</RelativeLayout>
比如:
String str1=“TEST1”;
字符串str2=“TEST2”;
TextView text=(TextView)vi.findViewById(R.id.text);
text.setText(str1);
text.append(Html.fromHtml(
); text.append(str2);

希望它能起作用:)

如果您添加了一些关于您正试图实现的目标的信息,我们可能会建议其他方案。但对于这个简单的问题,我的猜测是否定的。@Warpzit我补充了一些关于我试图实现的目标的信息。谢谢你的建议。正如我所说的,我希望在我的
文本视图中得到相同的结果,但它不起作用。我得到的只是大尺寸的“TEST1TEST2”。行未断开。请尝试减少textView.setWidth(100);是的,我试过了,效果很好。但这并不是我真正想要的,因为如果您将TEST1更改为TEST1,那么您将不得不再次更改宽度。谢谢你的快速回复。是的,这是一个解决办法。如果你想在动态文本中获得相同的结果,那么你需要动态计算宽度。这可能是我需要的,因为我的问题的答案似乎是“否”。我试试这个。非常感谢。你甚至不需要运行你的应用程序就可以看到它不工作(append是一个线索)。此外,我相信“检查并查看”比“希望它能工作”要好得多。谢谢你的回复)好的,好的,对我来说很好,我正在处理你的问题:但是最后的文本长度超过了10,不是吗?不!!!您认为
如何在字符串中添加类似“\n”的内容??文本长度不会改变,它只是在演示文稿中打断文本,而不是在其中添加新行。
String str1 = "TEST1";
String str2 = "TEST2";

TextView text=(TextView)vi.findViewById(R.id.text);
text.setText(str1);
text.append(Html.fromHtml(< br>));
text.append(str2);