0

TableLayout在我的 android 应用程序中有一个。我是新手,TableLayout's所以我不知道如何在我的 .java 文件的所有行中插入数据。
这是我的 xml 文件,其中包含TableLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
            <TableRow android:id="@+id/tabla_cabecera" android:layout_width="match_parent" android:layout_height="match_parent"></TableRow>
            <TableLayout android:id="@+id/tabla_cuerpo" android:layout_height="match_parent" android:layout_width="match_parent">
                <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
                <TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView></TableRow>

                <TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView  android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
                <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content">                    

                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>
                    <TextView  android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView"></TextView>

                </TableRow>
            </TableLayout>


</LinearLayout>

我知道如何膨胀布局以及如何简单地插入数据TextView

View v = inflater.inflate(R.layout.subject_report_layout, container,false);
TextView tv = (TextView ) v.findViewById(R.id.tv);
TableRow tr = (TableRow) v.findViewById(R.id.tableRow2);
tv.setText("Text");

如何对表格的所有行和列执行相同操作?

4

1 回答 1

2

您必须在 XML 文件中定义 TableLayout,如下所示:

<TableLayout
                    android:id="@+id/tableview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/bg_listview"
                    android:cacheColorHint="#00000000"
                    android:divider="@android:color/transparent" >
                </TableLayout>

在您的 Java 文件中,您可以在下面进行编码:

tableview = (TableLayout) findViewById(R.id.tableview);
    tableview.setPadding(15, 3, 15, 3);

    for (int i = 0; i <10; i++) {

            TableRow row = new TableRow(this);

            TableLayout.LayoutParams lp = new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.FILL_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(lp);

            row.setPadding(15, 3, 15, 3);

            row.setBackgroundColor(Color.parseColor("#E4CC91"));


            TextView Header = new TextView(this);

            Header.setGravity(Gravity.CENTER);
            Header.setText("YOUR_TEXT_HEADER");
            Header.setTextSize(25.0f);
            Header.setPadding(15, 0, 15, 0);
            Header.setTextColor(Color.parseColor("#FFFFFF"));
            Header.setTypeface(null, Typeface.BOLD);

            row.addView(Header);

            for (int j = 0; j < 5; j++) {
                TextView Values = new TextView(this);
                Values.setPadding(15, 0, 15, 0);
                Values.setGravity(Gravity.CENTER);
                Values.setTextSize(25.0f);
                Values.setTextColor(Color.parseColor("#FFFFFF"));
                Values.setTypeface(null, Typeface.BOLD);
                Values.setText("YOUR_TEXT_RAW");
                row.addView(Values);
            }
            tableview.addView(row);
    }
于 2012-11-07T13:46:31.523 回答