如何使用java代码在android中创建多列表视图?

时间:2011-05-21 08:28:45

标签: android tableview

我正在开发一个Android应用程序。为此,我需要一个动态的tableView。我正在使用TableLayout来获取android中可用的内容。但我找不到在tableView中有多个列的方法。有什么选择吗?

2 个答案:

答案 0 :(得分:16)

我不知道我是否完全理解你的问题,但在这里:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    TableLayout tableLayout = new TableLayout(getApplicationContext());
    TableRow tableRow;
    TextView textView;

    for (int i = 0; i < 4; i++) {
        tableRow = new TableRow(getApplicationContext());
        for (int j = 0; j < 3; j++) {
            textView = new TextView(getApplicationContext());
            textView.setText("test");
            textView.setPadding(20, 20, 20, 20);
            tableRow.addView(textView);
        }
        tableLayout.addView(tableRow);
    }
    setContentView(tableLayout);
}

此代码创建包含3列和4行的TableLayout。基本上你可以在XML文件中声明TableLayout,然后将setContentView声明为XML,并使用findViewById找到你的TableLayout。只有TableRow和它的孩子必须在java代码中完成。

答案 1 :(得分:0)

您可以在设计视图中添加任意数量的列。您所要做的就是将要显示的任何View元素放在TablaRow标记之间的列中。

<TableLayout>
  <TableRow>
    <TextView></TextView> // That's a column
    <ImageView></ImageView>  // That's other column
    ....

    <Other views></Other views> // That's the last column
  </TableRow>
 </TableLayout>
相关问题