Views added to TableLayout are not programmatically displayed

I am trying to add views to TableLayout, but they just won't appear (I'm testing an emulator). Actually I wrote more code than this, i.e. Adding text views to tablerows, but I cut it down to this, and even that won't work. Any idea why?

Here's the test.xml code:

 <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TEST" > </TextView> </TableRow> </TableLayout> 

And the java code:

 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.test); /* Find Tablelayout defined in test.xml */ TableLayout tl = (TableLayout) findViewById(R.id.tableLayout); /* Create a new row to be added. */ TableRow tr = new TableRow(this); tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); /* Create a Button to be the row-content. */ Button b = new Button(this); b.setText("Dynamic Button"); b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); /* Add Button to row. */ tr.addView(b); /* Add row to TableLayout. */ tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); } 

When I test the emulator, I see only a text image with a hard-coded string "TEST" (defined in the XML file).

+4
source share
1 answer

Do not try to set the Layout button or add TableLayout.LayoutParams when adding a row to the table.

 //b.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); tr.addView(b); //tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); t1.addView(tr) 

I think TableLayout.LayoutParams is causing some problems.

+11
source

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


All Articles