在Android中,TableLayout是一种用于创建表格布局的布局容器。可以使用TableLayout来创建包含多行多列的表格,并在每个单元格中放置其他视图或控件。
以下是使用TableLayout的基本步骤:
- 在XML布局文件中添加TableLayout标签:
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
- 在Activity中获取TableLayout实例:
TableLayout tableLayout = findViewById(R.id.tableLayout);
- 创建TableRow并将其添加到TableLayout中:
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT
);
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(rowParams);
tableLayout.addView(tableRow);
- 在TableRow中添加单元格(TableCell)并设置内容:
TextView textView = new TextView(this);
textView.setText("Cell 1");
tableRow.addView(textView);
TextView textView2 = new TextView(this);
textView2.setText("Cell 2");
tableRow.addView(textView2);
-
可以重复步骤4以创建更多的单元格,并将它们添加到同一行的TableRow中。
-
可以重复步骤3和步骤4以创建更多的行,并将它们添加到TableLayout中。
TableRow tableRow2 = new TableRow(this);
tableRow2.setLayoutParams(rowParams);
tableLayout.addView(tableRow2);
...
通过重复步骤4和步骤5,可以创建任意数量的行和单元格,并在TableLayout中进行布局。
提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。
邮箱:yvsm@163.com
微信:yvsm316
QQ:316430983
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!