4

次のコードを使用して改行できます。

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 です。つまり、各行1 文字としてカウントされます。

  • 各段落はtextの最後の文字で終了します ( '\n' ではありません)

  • いくつかのパラメーターがあります( BulletList 、 Numeric list 、 Paragraph など)

私は自分のTextViewデータとソースデータの間の厳密なマッピングが必要です。つまり、私のカーソル位置ごとに、TextViewソースデータでその前にある文字数を数える必要があります。

4

4 に答える 4

0

2 つの TextView を取り、1 つを下に追加します。その後、長さの問題は見つかりません。

 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 に答える