0

我需要在我的应用程序中动态地将一些表格添加到线性布局中。我写了这段代码:

LinearLayout tabella = (LinearLayout) findViewById(R.id.tabella_contatori);

    for(int i =0; i<array_list.size(); i++){
        TableRow row = new TableRow(getApplicationContext());
        row.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        TextView data = new TextView(getApplicationContext());
        data.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.2f));
        data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
        data.setTextColor(Color.BLACK);
        data.setBackgroundColor(Color.WHITE);
        data.setPadding(2, 0, 0, 0);
        data.setText("asd");

        row.addView(data);
        tabella.addView(row);
    }
}

但是当我打开应用程序时,什么也没有出现。我已经检查了 array_list.size 是否大于 0。我该怎么办?谢谢, 马蒂亚

4

2 回答 2

3

问题出在您的 TextView 布局参数中。类型应该是 TableRow.LayoutParams 而不是 LinearLayout.LayoutParams。 data.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));

于 2012-04-13T14:19:58.557 回答
1

采用表格布局并尝试在您的 main.xml 中使用它...

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTableLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <TableRow
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">

          <TextView android:text="Some Text"/>

     </TableRow>
</TableLayout>

在您的活动中

this.setContentView(R.layout.main);

/* Find Tablelayout defined in main.xml */
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayout);
     /* Create a new row to be added. */
     TableRow tr = new TableRow(this);
     tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
          /* Create a TextView to be the row-content. */    

        TextView data = new TextView(getApplicationContext());
        data.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.2f));
        data.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
        data.setTextColor(Color.BLACK);
        data.setBackgroundColor(Color.WHITE);
        data.setPadding(2, 0, 0, 0);
        data.setText("asd");

          /* Add TextView to row. */
          tr.addView(data);
    /* Add row to TableLayout. */
    tl.addView(tr,new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT));
于 2012-04-13T14:10:27.377 回答