android studio制作表格
时间: 2023-12-15 11:33:00 浏览: 212
android 表格
以下是在Android Studio中制作表格的步骤:
1. 在XML布局文件中添加TableLayout和TableRow标签,如下所示:
```xml
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Column 1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Column 2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Column 3" />
</TableRow>
</TableLayout>
```
2. 在Java代码中获取TableLayout和TableRow对象,并动态添加表格行和单元格,如下所示:
```java
TableLayout tableLayout = findViewById(R.id.tableLayout);
for (int i = 0; i < 5; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TextView textView1 = new TextView(this);
textView1.setText("Row " + i + " Column 1");
textView1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textView2 = new TextView(this);
textView2.setText("Row " + i + " Column 2");
textView2.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
TextView textView3 = new TextView(this);
textView3.setText("Row " + i + " Column 3");
textView3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow.addView(textView1);
tableRow.addView(textView2);
tableRow.addView(textView3);
tableLayout.addView(tableRow);
}
```
3. 运行应用程序,即可看到表格已经被添加到界面中。
阅读全文