Android中的表我做错了什么?

时间:2011-03-18 13:17:28

标签: android android-emulator android-tablelayout dynamic-tables

我没有收到任何错误,但它没有在模拟器中显示任何内容。我想要一张动态表...

xml文件的内容:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:stretchColumns="0,1"
                android:id="@+id/maintable" >

        </TableLayout>

添加tablerows和textviews的活动中的代码:

 TableLayout tl = (TableLayout) findViewById(R.id.maintable);

                // Go through each item in the array
                for (int current = 0; current < strAuftraege.length; current++)
                {
                    // Create a TableRow and give it an ID
                    TableRow tr = new TableRow(this);
                   // tr.setId(100+current);
                    tr.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));   

                    // Create a TextView to house the name of the province
                    TextView labelTV = new TextView(this);
                  //  labelTV.setId(200+current);
                    labelTV.setText(strAuftraege[current][0]);
                   // labelTV.setTextColor(Color.BLACK);
                    labelTV.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    tr.addView(labelTV);

                    // Create a TextView to house the value of the after-tax income
                    TextView valueTV = new TextView(this);
                    valueTV.setId(current);
                    valueTV.setText(strAuftraege[current][1]);
                    System.out.println(valueTV.getText().toString());


                   // valueTV.setTextColor(Color.BLACK);
                    valueTV.setLayoutParams(new LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                    tr.addView(valueTV);

                    // Add the TableRow to the TableLayout
                    tl.addView(tr, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));
                }

我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

将TextViews添加到表格行时,可能需要使用TableRow.LayoutParams?您目前正在使用普通的LayoutParams。

答案 1 :(得分:1)

使用

tr.addview(tv1,new TableRow.LayoutParam(
      android.widget.TableLayout.LayoutParam.FILLPARENT,
      android.widget.TableLayout.LayoutParam.WILLPARENT) 

取代

tr.addview(tv1,new LayoutParam(
      Layoutparam.FILLPARENT,
      LayoutParam.WRAPPARENT);

它将解决您的问题。