android中table怎么使用
导读:在Android中,可以通过TableLayout和TableRow来创建表格布局。以下是一个简单的示例: 首先在xml布局文件中定义TableLayout和TableRow: <TableLayout android:i...
在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);
// 创建一个新的TableRow
TableRow 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);
通过这种方式,可以动态的添加行数据到表格布局中。可以根据需要添加更多的行数据来展示更多的内容。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: android中table怎么使用
本文地址: https://pptw.com/jishu/672616.html