Android 的表格布局可以使用 GridView 或者 TableLayout 来实现,可以通过设置列数来控制表格的布局。
使用 GridView:
1. 在 XML 布局文件中定义 GridView,设置列数:
```
android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" /> ``` 2. 在代码中找到 GridView,并设置 Adapter: ``` GridView gridView = findViewById(R.id.gridview); gridView.setAdapter(adapter); ``` 3. 创建一个 Adapter,实现对 GridView 中每个单元格的操作。 使用 TableLayout: 1. 在 XML 布局文件中定义 TableLayout,并设置列数: ``` android:id="@+id/tablelayout" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="*"> android:layout_column="1" android:text="Column 1" /> android:layout_column="2" android:text="Column 2" /> android:layout_column="3" android:text="Column 3" /> ``` 2. 通过代码动态添加行和列: ``` TableLayout tableLayout = findViewById(R.id.tablelayout); TableRow tableRow = new TableRow(this); tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); TextView column1 = new TextView(this); column1.setText("Column 1"); tableRow.addView(column1); TextView column2 = new TextView(this); column2.setText("Column 2"); tableRow.addView(column2); TextView column3 = new TextView(this); column3.setText("Column 3"); tableRow.addView(column3); tableLayout.addView(tableRow); ``` 通过以上方式,可以实现不同的列数来控制表格布局。