4

レイアウトに 3 つのテキストが必要です。中央のtextView1 つは赤で、他の 2 つは黒です。だから私はそれをaに追加し、textView1の右側にtextView2として、textView2の右側にtextView3をrelativelayout設定しましたlayout

これは私がやったことです

しかし、3 番目のテキスト ビューのテキストが大きい場合は、テキスト ビュー 1 にする必要があります。私はこれを手に入れました。

3番目のテキストが長い場合、これを取得します

今、私はこのようなものを得るために何をすべきですか?

これは私が必要なものです

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/rel_lay"
        android:background="#DFDFDF"
        android:padding="2dip" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="2dip"
            android:text="TextView"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/textView1"
            android:text="TextView"
            android:textColor="#FF0000" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="false"
            android:layout_toRightOf="@+id/textView2"
            android:text="TextView when the third text is longer"
            android:textColor="#000000" />

    </RelativeLayout>

これは、3 つの textView を使用した相対的なレイアウトです。

4

2 に答える 2

1

1 つを使用TextViewして、必要に応じてテキストを設定するだけです。赤色に設定するテキストはわかっています。したがって、必要なのは開始テキストと終了テキストのインデックスです。

次のコードを使用します。

SpannableString spanText = new SpannableString("My Name is Chintan Rathod");
spanText.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 5 /*start index*/, 10 /*end index*/, 0);
textView.setText(spanText);

ここでは510を渡していますが、独自のインデックスを渡すこともできます。ただし、IndexOutOfBoundExceptionが発生しない範囲内のインデックスを確認してください

于 2013-03-05T07:17:05.877 に答える
1

Spannedクラスと 1 つの TextViewを使用すると、運が良くなる可能性があります。あなたがそれを実装しようとしている方法では、TextView #3 は幅をfill_parent にする必要があり、TextView #2 がどこで終了するかを知ることができます。

于 2013-03-05T07:01:34.813 に答える