I am trying to populate a tablelayout with cells and rows to fill the entire screen. Like this:
http://imageshack.us/photo/my-images/69/device20120201005942.png/
This layout, designed in a graphic layout window in Eclipse, gives what I want (android: layout_weight solves vertical stretching):
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:stretchColumns="*" > <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1"> <ImageView android:id="@+id/imageView1" android:layout_gravity="center" android:src="@drawable/add" /> <ImageView android:id="@+id/imageView2" android:layout_gravity="center" android:src="@drawable/bound" /> <ImageView android:id="@+id/imageView3" android:layout_gravity="center" android:src="@drawable/cod" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" > <ImageView android:id="@+id/imageView4" android:layout_gravity="center" android:src="@drawable/delete" /> <ImageView android:id="@+id/imageView5" android:layout_gravity="center" android:src="@drawable/delete2" /> <ImageView android:id="@+id/imageView6" android:layout_gravity="center" android:src="@drawable/floppy" /> </TableRow> </TableLayout>
I am trying to create the same look and feel in the code, but so far have failed. Here is a link to my activity:
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tablelo); TableLayout tl = (TableLayout) findViewById(R.id.table); Log.d(TAG, "Width :" + tl.getWidth()); for (int row = 0; row < 3; row++) { TableRow tr = new TableRow(this); tr.setId(100 + row); for (int cell = 0; cell < 3; cell++) { ImageView image = new ImageView(this); image.setImageResource(imageIDs[row * 3 + cell]); tr.addView(image); } tr.setGravity(Gravity.CENTER); LayoutParams params = new TableRow.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1); tr.setLayoutParams(params); tl.addView(tr, params); } tl.setStretchAllColumns(true); }
I do not have enough code to install ImageView gravity, but this attemp is crashing:
image.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));
This code leads to the creation of three lines, each of which is stretched horizontally, but not vertically:
http://imageshack.us/photo/my-images/835/gridc.png/
I can not find what is missing here. I also tried to prepare tablerow in the designer and inflate and add to the table at runtime, but the result did not change.
So, somehow I want to get the same layout programmatically that I could create using an editor.
Help me please!