您将必须创建一个包含多行的 tableView,这些行将在运行时添加。第一行将包含列标题。
前任。
<TableLayout android:id="@+id/class">
<TableRow android:id="@+id/headers">
<TextView android:id="@+id/branch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif">
<TextView android:id="@+id/branch_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif">
<TextView android:id="@+id/branch_phone_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif">
<TextView android:id="@+id/branch_timings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif">
</TableRow>
</TableLayout>
并将数据添加到该表中,在类中,您将添加如下内容:
TableLayout layout = (TableLayout)LayoutInflater.from(canvas.getContext()).inflate(R.layout.class, null);
TableRow headerRow = (TableRow) layout.findViewById(R.id.header);
TextView branchName = (TextView) layout.findViewById(R.id.branch_name);
TextView branchNo = (TextView) layout.findViewById(R.id.branch_no);
TextView branchPhoneNo = (TextView) layout.findViewById(R.id.branch_phone_no);
TextView branchTimings = (TextView) layout.findViewById(R.id.branch_timings);
branchName.setText("Branch Name");
branchName.setTypeFace(null, Typeface.BOLD);
branchNo.setText("Branch No");
branchNo.setTypeFace(null, Typeface.BOLD);
branchPhoneNo.setText("Branch Phone No");
branchPhoneNo.setTypeFace(null, Typeface.BOLD);
branchTimings.setText("Branch Timimgs");
branchTimings.setTypeFace(null, Typeface.BOLD);
for (int i=0; i<noOfRows; i++) {
TableRow dataset = new TableRow(context);
TextView branchName = new TextView(context);
TextView branchNo = new TextView(context);
TextView branchPhoneNo = new TextView(context);
TextView branchTimings = new TextView(context);
branchName.setText("SampleBranchName" + i);
branchNo.setText("SampleNumber" + i);
branchPhoneNo.setText("SamplePhoneNumber" + i);
branchTimings.setText("SampleTimings" + i);
dataset.addView(branchName);
dataset.addView(branchNo);
dataset.addView(branchPhoneNo);
dataset.addView(branchTimings);
layout.addView(dataset);
}