2012-03-15 12 views
0

これは簡単な質問ですが、私はアンドロイドでは新しいので、どうやってやるのか分かりません。アンドロイドでtextviewsを分ける方法は?

私は2つの相対的なレイアウトを持っていますが、最初にTextViewが大きくなるとそれを避けたいもう1つの書き込みをしますTextView。どうしたらいいですか?

<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:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="first" 
       android:gravity="left"/> 

    <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="second" 
       android:layout_alignParentRight="true"/> 

</RelativeLayout> 

ただし、text = firstfirstfirst....が第2のtextviewを上書きする場合。

+0

アンドロイド:layout_toLeftOfまたはtoRightOf属性を使用する –

答えて

3

あなたはTextViewsのIDを与える必要があります。 RelativeLayoutを使用しているので、layout_toLeftOflayout_toRightOfを使用できますが、そのうちの1つを使用する必要があります。

<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:layout_weight="1" 
    android:layout_toLeftOf="@+id/textView2" 
    android:gravity="left" 
    android:text="firstfirstfirstfirstfirstfirsitseirodsifjdsoijfoisddsefdfadafdafad" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:layout_alignParentRight="true" 
    android:text="second" /> 

+0

はい、それであなたはアンドロイド:layout_alignParentLeft = "true"を追加する必要があります。ありがとう –

+1

しかし、右のテキストビューが長い場合は、左のテキストビューが消えます。 – idiottiger

+0

ええ、そうです。私は自分の答えを更新しました。物事を均等に配置するために、レイアウトの重みを追加する必要があります。 – adneal

0

いくつかのアドバイスを与える:

最初の左側のサイズを制御するためにandroid:maxWidth=""を使用して、左のTextViewの最大幅を設定することができますが、サイズよりもサイズが大きくなった場合は、テキストはこのようになりますが:text...

か、例えば、きれいに左右の領域を作る必要があります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:weightSum="2" > 

<TextView 
    android:id="@+id/left" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_weight="1" 
    android:text="firstdasssssssssssssssssssssssssssssssssssssssssssssssssssssssss" /> 

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_weight="1" 
    android:gravity="left" 
    android:text="secondsdasdasdarerewrwcxcadasdasdsadsdasdasdadadsd" /> 

android:weightSumのサイズを変更することもできます。

関連する問題