【发布时间】:2011-08-30 03:34:03
【问题描述】:
我正在开发一个安卓应用程序。为此,我需要一个动态的 tableView。我将 TableLayout 用于 android 中可用的内容。但是我找不到在我的 tableView 中有多个列的方法。请问有什么办法吗?
【问题讨论】:
我正在开发一个安卓应用程序。为此,我需要一个动态的 tableView。我将 TableLayout 用于 android 中可用的内容。但是我找不到在我的 tableView 中有多个列的方法。请问有什么办法吗?
【问题讨论】:
您可以在设计视图中添加任意数量的列。您所要做的就是将要显示的任何 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>
【讨论】:
我不知道我是否完全理解你的问题,但在这里:
@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 代码中完成。
【讨论】: