在Android中,可以通过TableLayout和TableRow来创建表格布局。以下是一个简单的示例:
首先在xml布局文件中定义TableLayout和TableRow:<TableLayout android:id="@+id/tableLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Age" /> </TableRow> <!-- 添加更多的TableRow来展示更多的数据 --></TableLayout>在Activity中找到TableLayout并动态添加行数据,示例代码如下:TableLayout tableLayout = findViewById(R.id.tableLayout);// 创建一个新的TableRowTableRow row = new TableRow(this);TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);row.setLayoutParams(layoutParams);// 添加TextView到新的TableRow中TextView nameTextView = new TextView(this);nameTextView.setText("Alice");row.addView(nameTextView);TextView ageTextView = new TextView(this);ageTextView.setText("25");row.addView(ageTextView);// 将新的TableRow添加到TableLayout中tableLayout.addView(row);通过这种方式,可以动态的添加行数据到表格布局中。可以根据需要添加更多的行数据来展示更多的内容。




