2013-07-13 34 views
5

我有一个RelativeLayout,其左侧为ImageView,右侧为TextViewTextView是通过他们的API从网站下载的,因此它的内容每次都会有所不同。在两个视图(或最低视图)下定位视图

我想有另一TextView这两个下方,但我有一个问题,当TextView长度小于ImageView。发生这种情况时,底部的TextView将与ImageView重叠,因为我将底部的TextView对齐到右上角的TextView以下。

我需要发生的事情是能够将底部TextView对齐到最低的视角。

这是我的布局XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/itemImageView" 
     android:layout_width="100dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/id_image" /> 

    <TextView 
     android:id="@+id/itemContentsTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/itemImageView" 
     android:layout_marginRight="2dp" 
     android:layout_marginTop="2dp" 
     android:text="Sample contents\nSample contents\nSample contents" /> 

    <TextView 
     android:id="@+id/itemIdTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/itemContentsTextView" 
     android:layout_marginBottom="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:text="1234" /> 

</RelativeLayout> 

回答

4

您应该创建LinearLayout作为顶级父并使其orientation:vertical。然后首先添加您的relativeLayout,然后添加您的TextView

所以它会像这样流口水。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 
    <RelativeLayout  
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

     <ImageView 
       android:id="@+id/itemImageView" 
       android:layout_width="100dp" 
       android:layout_height="80dp" 
       android:layout_alignParentLeft="true" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_marginTop="5dp" 
       android:src="@drawable/id_image" /> 

     <TextView 
       android:id="@+id/itemContentsTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignRight="@+id/itemImageView" 
       android:layout_marginRight="2dp" 
       android:layout_marginTop="2dp" 
       android:text="Sample contents\nSample contents\nSample contents" /> 
    </RelativeLayout> 
    <TextView 
      android:id="@+id/itemIdTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="5dp" 
      android:text="1234" /> 
</LinearLayout> 
3

裹在另一RelativeLayout顶部ImageViewTextView和使用,作为锚的底部TextView

像这样(未测试):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:id="@+id/wrappingLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/itemImageView" 
      android:layout_width="100dp" 
      android:layout_height="80dp" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:layout_marginTop="5dp" 
      android:src="@drawable/id_image" /> 

     <TextView 
      android:id="@+id/itemContentsTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignRight="@+id/itemImageView" 
      android:layout_marginRight="2dp" 
      android:layout_marginTop="2dp" 
      android:text="Sample contents\nSample contents\nSample contents" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/itemIdTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/wrappingLayout" 
     android:layout_marginBottom="10dp" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="5dp" 
     android:text="1234" /> 

</RelativeLayout> 
1

使用一个更RelativeLayout我有测试和文本无重叠

所以它应该是:

<RelativeLayout 
    android:id="@+id/temp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/itemImageView" 
     android:layout_width="100dp" 
     android:layout_height="80dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/itemContentsTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/itemImageView" 
     android:layout_marginRight="2dp" 
     android:layout_marginTop="2dp" 
     android:text="Sample contents\nSample contents\nSample contents" /> 
</RelativeLayout> 

<TextView 
    android:id="@+id/itemIdTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/temp" 
    android:layout_marginBottom="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="5dp" 
    android:text="1234" /> 

相关问题