表格布局未显示

时间:2012-11-26 21:20:08

标签: android dynamic for-loop tablelayout

我正在使用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]);
    }

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

1 个答案:

答案 0 :(得分:0)

TableRow宽度更改为match_parent

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

问题是你的行的宽度是由它的内容(wrap_content)定义的,而你的内容的宽度(行内的LinearLayout)是由父级定义的。

您还可以删除LinearLayout并将TextView项直接放在TableRow扩展LinearLayout

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

 TableRow.LayoutParams lRow=new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);