2016-02-26 32 views
1

我有一個TextView和一個ImageView,它們之間有一個空格。我希望ImageView位於TextView的右側,並且TextView可以改變其大小。我設法做到了這一點,但如果文本太長,textView將擴展到超出其父邊界的左側。這是爲什麼發生?我能做些什麼來解決它?爲什麼這個textView擴展到超出其父邊界的左側?

<LinearLayout 
    android:layout_height="match_parent" 
    android:layout_width="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="right|center_vertical"> 

    <TextView 
     android:id="@+id/received" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:padding="5dip" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:background="@drawable/received_style" 
     android:text="dsssssajhhhhhhhhhhhhhhhhhhhoooooooooohsssssssssdhsfsd" 
     /> 

    <Space 
     android:layout_width="5dp" 
     android:layout_height="0dp" 
     /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:src="@drawable/edit" /> 
</LinearLayout> 
+0

發現錯誤! LinearLayout的layout_width是「wrap_content」,而不是「match_parent」。現在代碼就像我的電腦上一樣。 – maf27

回答

0

爲TextView放置重量。

android:layout_weight="1" 
0

一個LinearLayout給每個孩子,因爲它要求至少儘可能多的空間。

您可以使用RelativeLayout而是對準ImageView在父權和TextViewImageView

+0

我不希望ImageView在父級右側對齊,但是dinamically位於textView的右側。 – maf27

0

你可以嘗試一些這樣的。

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

    <TextView 
     android:id="@+id/received" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:padding="5dip" 
    android:layout_toLeftOf="@+id/space" 
     android:text="dsssssajhhhhhhhhhhhhhhhhhhhoooooooooohsssssssssdhsfsd" 
     /> 

    <Space 
     android:id="@+id/space" 
     android:layout_width="5dp" 
     android:layout_height="0dp" 
     android:layout_toLeftOf="@+id/imageView" 
     /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="30dp" 
     android:layout_alignParentRight="true" 
     android:layout_height="30dp" 
     android:src="@drawable/edit" /> 
</RelativeLayout> 

希望能幫到你!