简体   繁体   中英

TextView Spanning 2 Rows with RelativeLayout

I am trying to create a list item that looks like this :-

| date | src | type | value |
|     description   | ..    |

I use the relative layout shown below :-

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

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </TextView>

    <TextView
        android:id="@+id/src"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/date"
        android:layout_alignParentTop="true">
    </TextView>

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/src"        
        android:layout_alignParentTop="true">
    </TextView>

    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="right"
        android:layout_toRightOf="@id/type"        
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:textAppearance="?android:attr/textAppearanceLarge"/>    

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/date"
        android:layout_toLeftOf="@id/value"
        android:layout_alignRight="@id/type"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true">
    </TextView>
</RelativeLayout> 

But what I get is more like this :-

| date | src | type |
|     description   | value |

I can't get the rightmost cell span two rows. Am I using it the wrong way or there is another right layout type for this.

Thanks

This worked for me:

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

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</TextView>

<TextView
    android:id="@+id/src"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/date">
</TextView>

<TextView
    android:id="@+id/type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/src">
</TextView>

<TextView
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_toRightOf="@+id/type"
    android:gravity="right"       
    android:layout_alignParentRight="true"
    android:textAppearance="?android:attr/textAppearanceLarge"/>   

<TextView
    android:id="@+id/description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/date"
    android:layout_toLeftOf="@id/value"
    android:layout_alignRight="@id/value">
</TextView>
</RelativeLayout> 

I tried the code you supplied and it works as written. See the image below (I added some color and text so I could actually see it in the GUI layout editor).

在此输入图像描述

However, I think a better implementation that might be more along the lines of what you are looking for would be this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </TextView>
    <TextView
        android:id="@+id/src"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/date" >
    </TextView>
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/src" >
    </TextView>
    <TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignBottom="@+id/description"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/type"
        android:background="@color/red"
        android:gravity="center|center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <TextView
        android:id="@id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@id/type"
        android:layout_below="@id/date"
        android:layout_toLeftOf="@id/value" >
    </TextView>
</RelativeLayout>

Which gives you this:

在此输入图像描述

I think you should use two LinearLayout(s) having assigned weights to the TextView(s)

Like This

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:weightSum="4" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:weightSum="2"

        android:layout_below="@+id/linearLayout1" >

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

    </LinearLayout>

</RelativeLayout>

Edit: this should work if you want predefined number of elements as you stated above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM