Okay, so I'm trying to insert TableRows
into a TableLayout
programmatically every time a button is tapped. Each of these rows contain a TextView
set to some String. My problem is, that although the TableRow
is inserted successfully, the text within the TextView that is itself inside the TableRow
won't show up.
Here is what I'm doing:
//prepare TextView to put inside a TableRow
TextView text = new TextView(MyClass.this);
text.setText(someString);
text.setTextSize(15);
text.setTextColor(Color.parseColor("#FF9900"));
text.setLayoutParams(new LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//Prepare TableRow to be inserted
TableRow row = new TableRow(MyClass.this);
row.addView(text);
row.setPadding(4,4,4,4);
row.setGravity(Gravity.CENTER);
row.setLayoutParams(new LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//add row to the table
someTableLayout.addView(row);
The result: A row is inserted, but I can't see the text (I know the row is inserted since I can see the dividers between each row). I'm guessing it has something to do with the LayoutParams
? I'm probably overlooking something very stupid... but I have no idea...
Screenshot after I click Roll
which should insert a row with the string/TextView:
The "Start By Rolling" TextView is the first row which I put in through the XML file. It displays fine, but the rows inserted programmatically don't
First this is you have a TextView
with height match_parent
and a tablerow
with height wrap_content
so make TextView
height to wrap_content as well.
And second assign appropriate LayoutParams for ex:
TextView text = new TextView(MyClass.this);
text.setText(someString);
text.setTextSize(15);
text.setTextColor(Color.parseColor("#FF9900"));
text.setLayoutParams(new TableRow.LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//Prepare TableRow to be inserted
TableRow row = new TableRow(MyClass.this);
row.addView(text);
row.setPadding(4,4,4,4);
row.setGravity(Gravity.CENTER);
row.setLayoutParams(new TableLayout.LayoutParams
(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//add row to the table
someTableLayout.addView(row);
Try this out:
text.setLayoutParams(new LayoutParams
(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
text.setLayoutParams(new LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.