Sunday, July 24, 2011

[android-developers] A little help with layout params in code.

Hi All,

I am hoping someone out there can give me an idea what I am doing
wrong. I wanted to create a table dynamically in my application,
which I did, but also I wanted to be able to set some of the layout
parameters, specifically the gravity and layout_gravity for a couple
text views and one image view.

I thought it would be pretty straight forward and tried the following
code:


if (v.getId()==R.id.add_row) {
mRowCount++;
TableLayout table = (TableLayout)findViewById(R.id.the_table);
TableRow row = new TableRow(this);
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
ImageView iv = new ImageView(this);
tv1.setText("Table row #"+Integer.toString(mRowCount));
tv1.setGravity(Gravity.LEFT);
//The following line causes my textview to
disappear
tv1.setLayoutParams(new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.LEFT));
row.addView(tv1);
iv.setImageResource(R.drawable.divider);
//And the line after this one does the same to
my imageview
iv.setLayoutParams(new
FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, Gravity.CENTER));
row.addView(iv);
tv2.setText("Add more text here.");
row.addView(tv2);
table.addView(row);
}

Unfortunately I've been unable to get this to work. The problem is
that whenever I assign the layout parameters to either my text view or
my image view, that view seems to not get added to my table row, or if
it does, its not at all visible. Does anybody out there have
experience formatting table row comments in code? I'd sure appreciate
being pointed in the right direction.

Thanks!

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment