我正在嘗試使用基於數組圖標的動態行數填充TableLayout,但是我無法獲得要在屏幕上顯示的行。該表應顯示一個5x3的圖標網格。我使用的代碼如下:如何在Android中使用動態行數創建表
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保持爲空。任何人都可以告訴我,如果我在正確的路線?
任何幫助,將不勝感激。
感謝
你能解釋一下你的TableLayout必須顯示?只有使用Image和TextView的行? – Jackyto
@Jahckyto TableLayout應該顯示三行填充圖標和一個帶有每個圖標名稱的TextView。使用GridView不是一個選項,因爲其他原因 – user2060733