在不增加文本长度的情况下断开一行

时间:2013-04-18 10:54:50

标签: 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内获得相同的结果而不会增加文字长度?

我想要实现的目标:

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

[{type: line, params: {line params}}, {type: text, params: {text params}, ...]
  • 开始时总是

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

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

  • 每个段落以 text 的最后一个字符结尾(不是'\ n')

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

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

4 个答案:

答案 0 :(得分:0)

取两个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>

答案 1 :(得分:0)

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);

希望它有效:)

答案 2 :(得分:-1)

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

答案 3 :(得分:-1)

嗯,有狡猾的方式

    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" />
相关问题