views:

76

answers:

2

I am having problems creating a table layout with 4 columns, that span horizontally.

I want my Row to look like this:

AAPL | 200.00 | Image Divider | 1.53 (+1.5%)

I want the 200.00 to be right aligned, as well as the 1.53(+1.5%).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/stocks_gradient" android:orientation="horizontal">
    <TableLayout android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/priceText" android:layout_height="wrap_content"
        android:id="@+id/TableLayout01">
        <TableRow android:layout_width="wrap_content"
            android:layout_below="@+id/nameText" android:id="@+id/TableRow01"
            android:layout_height="wrap_content"></TableRow>
        <TextView android:id="@+id/nameText" android:layout_height="wrap_content"
            android:padding="5dip" android:layout_alignParentLeft="true"
            android:text="Symbol" android:textStyle="bold" android:textSize="24sp"
            android:layout_width="100dp" android:textColor="#4871A8" />
        <TextView android:id="@+id/priceText" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:padding="5dip"
            android:layout_toRightOf="@+id/nameText" android:gravity="right"
            android:text="100" android:textStyle="bold" android:textSize="24sp"
            android:textColor="#4871A8" />
        <ImageView android:layout_toLeftOf="@+id/changeText"
            android:id="@+id/verticalDivider" android:background="@drawable/vertical_divider"
            android:layout_width="wrap_content" android:layout_height="48dp"></ImageView>
        <TextView android:id="@+id/changeText" android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/priceText" android:gravity="right"
            android:textSize="18sp" android:text="3.07(+1.08%)" android:padding="12dip"
            android:layout_width="fill_parent" />
    </TableLayout>
A: 

android:layout_toLeftOf, android:layout_toRightOf, and android:layout_below are attributes for use with RelativeLayout. You do not have a RelativeLayout. Hence, those attributes as used in your XML above are doing you no good.

To make a row with four columns, have four children of the TableRow widget.

I am not completely clear on how you would achieve your right-aligned effect. Probably android:gravity will get you there, but I have a suspicion that the problem is more complicated than that.

CommonsWare
Aren't my 4 layout items children of the TableRow right now?
Sheehan Alam
@Sheehan Alam: No. Note the position of your `</TableRow>` tag.
CommonsWare
A: 

android:gravity should be all you need here since your TextView is already in width=fill_parent, e.g. it will align the text to the right inside the view.

Otherwise you can also play with android:layout_gravity="right", e.g. if you have a view with width=wrap_content and you need to tell the container how to align it.

ralfoide