我正在尝试使用基于图标数组的动态行数填充 TableLayout,但是我无法让行显示在屏幕上。该表应显示一个 5x3 的图标网格。我正在使用的代码如下:
Services serv = new Services();
Item[] iconArray = serv.getIcons();
TableLayout table = (TableLayout) findViewById(R.id.table1);
int rowNum = iconArray.length/5;
TableRow rows[]= new TableRow[rowNum];
int count = 0;
for (int i = 0; i < iconArray.length; i++) {
final String name = iconArray[i].name;
final int icon = iconArray[i].icon;
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View v = inflater.inflate(R.layout.icon, null,false);
TextView text = (TextView) v.findViewById(R.id.text);
text.setText(name);
ImageView imageicon = (ImageView) v.findViewById(R.id.icon);
imageicon.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Do some stuff
}
});
Drawable drawicon = getResources().getDrawable(icon);
imageicon.setBackgroundDrawable(drawicon);
if(i == 4) //End of Row
{
count++;
}
if(i == 9) //End of Row2
{
count++;
}
rows[count] = new TableRow(this);
rows[count].addView(v);
table.addView(rows[count]);
}
这不会向屏幕输出任何内容,并且 TableLayout 保持为空。谁能告诉我我是否在正确的路线上?
任何帮助,将不胜感激。
谢谢