【发布时间】:2012-04-06 11:48:06
【问题描述】:
我希望你能帮助我。 我想动态创建表格(截图)。我通过下面的代码创建了它:
TableLayout tl = (TableLayout) findViewById(R.id.main_table);
FOR. 1-300.........
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.CYAN);
tr_head.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setId(20);
ImageButton xyz = new ImageButton(this);
xyz.setId(21);
xyz.setPadding(5, 5, 5, 5);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 20 );
rl.addView(xyz,params);
tr_head.addView(rl);
tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
END OF FOR.......
通过类似的代码,我很好地创建了 2 种类型的项目,一种用于类别(3 个子视图),另一种用于类别项目(10 个子视图)。 然后我使用下面的代码为按钮和整个项目分配 onclick 侦听器。:
int count = tl.getChildCount();
for(int i = 0; i < count; i++){
TableRow v = (TableRow)tl.getChildAt(i);
if(v.getChildAt(0) instanceof RelativeLayout){
RelativeLayout relativ = (RelativeLayout)v.getChildAt(0);
if(relativ.getChildCount()>5)
relativ.setOnClickListener(new MyClickListener());
...........
但是当我想创建包含 300 个项目的表时,需要 30 秒。在模拟器上呈现此视图。它真的很慢。所以我想问你如何渲染这个视图。一些示例或教程会很有帮助。
非常感谢。
【问题讨论】:
标签: android android-layout tablelayout