Populating TableLayout with android: layout_weight programmatically

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!

+4
source share
1 answer

I spent an hour and a half trying to figure out what was going on. And then I found this :) Here is a complete list of my TableActivity, note the use of different LayoutParams for rows and table images:

 import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.widget.*; import android.widget.TableLayout; import android.widget.TableRow; public class TableActivity extends Activity { private static final String TAG="TableActivity"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.table_activity); 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); //Note that you must use TableLayout.LayoutParams, //since the parent of this TableRow is a TableLayout TableLayout.LayoutParams params = new TableLayout.LayoutParams( TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f); for (int cell = 0; cell < 3; cell++) { ImageView image = new ImageView(this); image.setImageResource(android.R.drawable.btn_plus); //Note that here you must use TableRow.LayoutParams //since TableRow is the parent of this ImageView TableRow.LayoutParams imageParams = new TableRow.LayoutParams(); //And this is how you set the gravity: imageParams.gravity = Gravity.CENTER; image.setLayoutParams(imageParams); tr.addView(image); } tr.setLayoutParams(params); tl.addView(tr, params); } tl.setStretchAllColumns(true); } } 

table_activity.xml is just an empty table layout:

 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/table" android:layout_width="match_parent" android:layout_height="match_parent"> </TableLayout> 

So, simply put, the simplest solution would be to use the layouts defined in xml files :) But if you need to use JAVA, you must be careful that the king of LayoutParams you use, you should always use one parent view.

+5
source

Source: https://habr.com/ru/post/1394086/


All Articles