tags:

views:

146

answers:

2

Hi,

I have a ViewGroup and it has a few children. And one of them is a TextView ("+id/text"). In my code, I would like to know how can I add a new View or ViewGroup which will be positioned vertically aligned and below the TextView (+"id/text")?

Thank you.

I have followed the advice below and try to use TableLayout. As a test, I try to layout statically to make sure things are aligned correctly.

Here is my layout xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/panel" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight">

    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageButton
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/icon"
        android:layout_column="0" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:layout_column="1">

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"                    
            android:gravity="left" />
    </LinearLayout>
   </TableRow>
   <TableRow>
    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/icon"
        android:layout_column="1"/>
    </TableRow>
</TableLayout>

But when i run in on emulator. The ImageButton on the 2nd row, does not align vertically with the textView in 1st row. Any idea why?

A: 

Consider using a TableLayout as your ViewGroup. With a TableLayout you can programmatically add a child at a specific location.

View view = new ViewIWantToAdd();
TableLayout layout = (TableLayout)findViewById(R.id.table_layout);
layout.addView(view);

Would add a view at the bottom of the TableLayout. Typically you would use a TableRow as the contents of the TableView, but you can add any view.

Mayra
If I have TableLayout with 3 coloumns, can I add a child sometime to the bottom of the 1 column and then other times, add it to the bottom of 2 column?
hap497
I believe you need to add to the TableLayout a TableRow at a time. Within your TableRow you can configure which column items end up in. Check out the TableLayout documentation: http://developer.android.com/intl/fr/reference/android/widget/TableLayout.html
Mayra
A: 

If your layout is not so complex, use RelativeLayout developer.android.com/reference/android/widget/RelativeLayout.html
"A Layout where the positions of the children can be described in relation to each other or to the parent..."

When you insert a new child view, create a new LayoutParams then setRule http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)

You can put the child view below the textView with this developer.android.com/reference/android/widget/RelativeLayout.html#BELOW

:)

Bino