在两个视图(或最低视图)下定位视图

时间:2013-07-13 18:08:50

标签: android android-layout alignment

左边有RelativeLayout ImageView,右边有TextViewTextView是通过其网站从网站下载的,因此每次内容都会有所不同。

我希望在这两个下面有另一个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>

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:3)

将顶部ImageViewTextView换成另一个RelativeLayout并将其用作底部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>

答案 2 :(得分: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" />