4

I'm able to break a line using following code:

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

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

textView.setText(str1 + '\n' + str2);

But the final text length is equal to 11.

Question:

Is there any special character or method that will allow me to reach the same result inside my TextView without increasing text length?

What I'm trying to achieve:

I have a data format, which is stored in JSON. It looks like

[{type: line, params: {line params}}, {type: text, params: {text params}, ...]
  • There is always line at the start

  • Each paragraph begins with line ( so it acts like a line separator which is stored at the beginning of line, not at the end )

  • Size of each line equals to 1, i.e. each line counts as a single character

  • Each paragraph ends with text's last character ( not '\n' )

  • There are some line params ( like BulletList, Numeric list, Paragraph )

I need a strict mapping between my TextView and source data, i.e. for each cursor position in my TextView I need to count how many characters preceed it in source data.

4

4 回答 4

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

希望它有效:)

于 2013-04-19T07:03:04.307 回答
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>
于 2013-04-18T11:18:04.007 回答
-1

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

于 2013-04-18T11:05:25.120 回答
-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" />
于 2013-04-18T11:13:59.777 回答