我想在運行時向TableLayout添加行,但數據根本沒有顯示出來。動態添加行到TableLayout沒有顯示
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="10dp"
android:scrollbars="horizontal|vertical" >
<TableLayout
android:id="@+id/server_dataTable"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@color/color_data_table_header_background"
android:stretchColumns="*" >
</TableLayout>
</ScrollView>
</LinearLayout>
相關的代碼:
private TableLayout addRowToTable(TableLayout table, String[] data) {
Context context = this.getApplicationContext();
TableRow row = new TableRow(context);
TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams();
// Wrap-up the content of the row
rowParams.height = TableLayout.LayoutParams.WRAP_CONTENT;
rowParams.width = TableLayout.LayoutParams.WRAP_CONTENT;
for (int i = 0; i < data.length; i++) {
TableRow.LayoutParams columnParams = new TableRow.LayoutParams();
// wrap-up content of the row
columnParams.height = TableRow.LayoutParams.WRAP_CONTENT;
columnParams.width = TableRow.LayoutParams.WRAP_CONTENT;
// set gravity to center of the column
columnParams.gravity = Gravity.CENTER;
TextView column = new TextView(context);
column.setText(data[i]);
row.addView(column, columnParams);
}
table.addView(row, rowParams);
return table;
}
我打電話從我活動的onCreate()addRowToTable
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
// Show the Up button in the action bar.
setupActionBar();
TableLayout dataTableHeader = (TableLayout) findViewById(R.id.server_dataTable);
dataTableHeader = addRowToTable(dataTableHeader, headerLabels);
}
我已經在這了現在大約40分鐘,已經閱讀了幾乎所有關於StackOverflow的相關問題,但都沒有找到任何有用的。任何幫助/指針都會很棒!
編輯:添加了活動的onCreate()。
你的'addRowToTable'運行在UI線程上嗎? –
是啊!我應該線程嗎? –
發佈您的所有代碼 –