2012-09-06 40 views
0

我有一個RelativeLayout,其中我將一個TextView放置在一個按鈕的左側,該按鈕被分配了父權。所以,當在TextView中顯示的文本比wrap_Content增加的多時,它會在Button上重疊。所以,我需要一個解決方案來在下一行顯示超出的文本。 任何人都可以幫助我解決這個問題?
在下面的佈局中,如果TextView2的文本是「11111111111111111111111」,它將與TextView1重疊。如何防止呢?
我的佈局如下: 如何動態調整Android TextView的寬度

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_margin="10dp" 
    android:scaleType="fitXY" 
    android:src="@drawable/icon" > 
    </ImageView> 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView1" 
    android:layout_margin="5dp" 
    android:text="TextView" 
    android:textSize="22dp" 
    android:layout_toRightOf="@id/imageView1" 
    > 
     </TextView> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:focusable="false" 
    android:text="Button" 
    android:layout_alignTop="@+id/imageView1" 

    > 
</Button> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/button1" 
    android:text="TextView222" 
    android:textSize="22dp" 
    android:layout_margin="5dp" 
    android:layout_alignTop="@+id/imageView1" 
    android:ellipsize="end" 
    > 
</TextView> 


在上面的佈局,如果TextView2的文字是 「11111111111111111111111」 將與TextView1重疊。如何防止

回答

1

只需將android:layout_toLeftOf="@+id/button"添加到您的TextView中,以防止它與您的Button發生衝突。

它看起來是這樣的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@+id/button" 
     android:text="11111111111111111111111111111111111111111111111111111111111111111111111111111111111"/> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:text="Potatoe"/> 

</RelativeLayout> 

更改了問題...但我會太回答這個新的。你可能會試圖將太多排成一行。然而,讓我們改變你的佈局水平的LinearLayout,並使用一對夫婦layout_weight屬性:

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

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:scaleType="fitXY" 
     android:src="@drawable/ic_launcher"/> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:text="111111111111111111111111111111111" 
     android:textSize="22dp"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     android:text="222222222222222222222222222222222" 
     android:textSize="22dp"/> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:text="Button"/> 

</LinearLayout> 

作爲一個說明:textView2,因爲你使用的ellipse屬性不環繞到一個新行。祝你好運!

+0

在頂部添加了我的佈局。 – user1276092

+0

在上面的佈局中,如果TextView2的文本是「11111111111111111111111」,它將與TextView1重疊。如何防止呢? – user1276092

+0

同樣的方法。添加'layout_toLeftOf'和'layout_toRightOf'以防止一個TextView寫在另一個之上。 – Sam

相關問題