抱歉Newb-ness。
我想在LinearLayout
(垂直)中创建一个视图元素列表。我创建了一个名为“category_list.xml”的<{1}}的xml布局
TableLayout
我想迭代一个数组,在每次迭代时创建一个新的TableLayout视图并将其添加到LinearLayout。我缺少的是基于上面的xml创建一个新的TableLayout。
像
这样的东西<TableLayout>
<TableRow>
<ImageView />
<TextView />
<CheckBox />
</TableRow>
</TableLayout>
有人能指出我正确的方向吗?以编程方式生成TableLayout更好吗?
答案 0 :(得分:3)
或者使用静态View.inflate函数
TableLAyout t = (TableLayout) View.inflate(this, R.layout.category_list, null);
无论如何,要小心在应用程序中膨胀和删除太多视图,因为短期对象会泄漏内存。考虑使用ListView with和Adapter。
答案 1 :(得分:2)
您想使用LayoutInflater
来“膨胀”xml文件。您可以使用getLayoutInflater()
在活动中获取LayoutInflater
。以下是它的工作原理(假设您的LinearLayout
的ID是“父”):
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
LayoutInflater inflater = getLayoutInflater();
TableLayout t = (TableLayout) inflater.inflate(R.layout.category_list, parent);