TableLayout是一個用于顯示表格數據的布局容器,它可以讓子視圖按行和列進行排列。下面是TableLayout的基本使用方式:
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
</TableLayout>
TableLayout tableLayout = findViewById(R.id.tableLayout);
TableRow row1 = new TableRow(context);
TextView textView1 = new TextView(context);
textView1.setText("Item 1");
row1.addView(textView1);
TextView textView2 = new TextView(context);
textView2.setText("Item 2");
row1.addView(textView2);
tableLayout.addView(row1);
TableRow row2 = new TableRow(context);
TextView textView3 = new TextView(context);
textView3.setText("Item 3");
row2.addView(textView3);
TextView textView4 = new TextView(context);
textView4.setText("Item 4");
row2.addView(textView4);
tableLayout.addView(row2);
tableLayout.setVisibility(View.VISIBLE);
通過以上步驟,就可以將數據按表格的形式顯示在TableLayout中。