0

我正在使用 java 代码开发动态表格布局

第一行在 xml 中

<TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:stretchColumns="*"
            android:id="@+id/TableMain" >

            <TableRow
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dip"
                android:background="#000000" >

                <LinearLayout
        android:id="@+id/lTable11"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_margin="1dip"
                    android:layout_weight="2"
                    android:background="#C0C0C0"
                    android:text="Grade" 
                    android:gravity="right"
                    android:padding="5dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_margin="1dip"
                    android:layout_weight="2"
                    android:background="#C0C0C0"
                    android:text="Hourse"
                    android:gravity="right" 
                    android:padding="5dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_margin="1dip"
                    android:layout_weight="6"
                    android:background="#C0C0C0"
                    android:text="Subject Name"
                    android:gravity="right"
                    android:padding="5dp" 
                      />

                    </LinearLayout>

            </TableRow>
        </TableLayout>

剩余的行在 java 部分中,它在 xml 中具有相同的属性

TextView tvRowGrades[]=new TextView[gotSubName.length];
    TextView tvRowHourse[]=new TextView[gotSubName.length];
    TextView tvRowSubName[]=new TextView[gotSubName.length];

    TableRow row[]=new TableRow[gotGradeItem.length];
    LinearLayout LRow[]=new LinearLayout[gotGradeItem.length];
    tableMain=(TableLayout)findViewById(R.id.TableMain);

    for(int i=0;i<gotHourse.length;i++){
        row[i]=new TableRow(this);
        TableRow.LayoutParams lRow=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
        lRow.setMargins(2, 2, 2, 2);
        row[i].setLayoutParams(lRow);
        row[i].setBackgroundColor(Color.BLACK);

        LinearLayout.LayoutParams lpLRow=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        LRow[i]=new LinearLayout(this);
        LRow[i].setLayoutParams(lpLRow);
        LRow[i].setGravity(Gravity.CENTER);
        LRow[i].setOrientation(LinearLayout.HORIZONTAL);

        tvRowGrades[i]=new TextView(this);
        tvRowGrades[i].setText(gotGradeItem[i]);
        android.widget.LinearLayout.LayoutParams lpTvOther=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT, 2);
        lpTvOther.setMargins(1, 1, 1, 1);
        tvRowGrades[i].setPadding(5, 5, 5, 5);
        tvRowGrades[i].setGravity(Gravity.RIGHT);
        tvRowGrades[i].setLayoutParams(lpTvOther);
        LRow[i].addView(tvRowGrades[i]);

        tvRowHourse[i]=new TextView(this);
        tvRowHourse[i].setText(gotHourse[i]+"");
        tvRowHourse[i].setPadding(5, 5, 5, 5);
        tvRowHourse[i].setGravity(Gravity.RIGHT);
        tvRowHourse[i].setLayoutParams(lpTvOther);
        LRow[i].addView(tvRowHourse[i]);

        tvRowSubName[i]=new TextView(this);
        tvRowSubName[i].setText(gotSubName[i]);
        android.widget.LinearLayout.LayoutParams lpTvSubName=new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT, 6);
        lpTvSubName.setMargins(1, 1, 1, 1);
        tvRowSubName[i].setPadding(5, 5, 5, 5);
        tvRowSubName[i].setGravity(Gravity.RIGHT);
        tvRowSubName[i].setLayoutParams(lpTvSubName);
        LRow[i].addView(tvRowSubName[i]);

        row[i].addView(LRow[i]);
        tableMain.addView(row[i]);
    }

当我运行应用程序时,表格没有显示,我想解决这个问题

4

1 回答 1

0

TableRow宽度更改为 match_parent

           <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dip"
            android:background="#000000" >

问题是你的行的宽度是由它的内容定义的wrap_contentLinearLayout

您也可以删除LinearLayout并将TextView项目直接放在TableRow扩展LinearLayout中。

对于您在代码中添加的其他行,您还应该更改LayoutParams

 TableRow.LayoutParams lRow=new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
于 2012-11-26T21:22:38.593 回答