2010-08-19 86 views
0

我想在表格中创建5种不同类型的单元格以及标识符,并根据给定的数据适当地加载它们,具体取决于类型? 在TableLayout中创建TableRow似乎是其中一个选项,但是如何根据类型动态创建tableRows?动态加载表格中的自定义表格单元

Thanx提前。

回答

0

你能检测执行时间类型吗?如果是的话,它应该直接使用开关或if结构。

要在运行时膨胀的XML资源依赖于该行使用类型:

((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)) 
.inflate(layoutId, yourTableLayout, true); 

设置此时,相应的膨胀的资源,然后进行前layoutId。参数yourTableLayouttrue只是我的猜测,请检查文档LayoutInflater并选择适合您需要的充气方法。

要创建TableRows动态,这个教程可以帮助:Creating TableRow rows inside a TableLayout programatically

基本上:

1-抓取TableLayout和创建的TableRow

// Get the TableLayout 
TableLayout tl = (TableLayout) findViewById(R.id.maintable); 

TableRow tr = new TableRow(this); 
tr.setId(100+current); 
tr.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

2-创建元素添加

TextView labelTV = new TextView(this); 
labelTV.setId(200+current); 
labelTV.setText(provinces[current]); 
labelTV.setTextColor(Color.BLACK); 
labelTV.setLayoutParams(new LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
tr.addView(labelTV); 

3-的TableRow添加到TableLayout

// Add the TableRow to the TableLayout 
tl.addView(tr, new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

似乎是方便,快捷,我没有寿进行了测试。

+0

由于tableRows是不同类型[5个不同类型],添加的元素是不同的。我想用一些标识符将其保存在xml中,并在运行时加载适当的标识符。可能吗? – neha 2010-08-19 13:03:25

+0

我已经更新了答案,我希望澄清你的问题。 – Maragues 2010-08-20 08:49:45

相关问题