2016-02-26 96 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> 

希望能帮到你!