我還是新的android編程,我想要做的是,我想創建5x5尺寸TableLayout。我知道這可以通過使用GridView BaseAdapter起訴Inflate服務來完成。但是對於這個我嘗試應用使用表格佈局。以下是代碼。我創建了TableLayout的新實例和Table行的新實例。在每個表格行上,我創建了5個TextView的實例。但是一旦我在模擬器或手機中打開,就沒有創建TableRow,它只是空白的表格佈局。也沒有拋出異常。5x5尺寸的Android桌面佈局
GridView navIcon =(GridView)findViewById(R.id.content); navicon.setAdapter(new ImageAdapter(this));
navIcon.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
// TODO Auto-generated method stub
if (position==0){
try{
TableLayout calgrid = (TableLayout) findViewById(R.id.gridtable);
Context ctxt = v.getContext();
TextView[] tView = new TextView[25];
calgrid = new TableLayout(ctxt);
int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1, getResources().getDisplayMetrics());
int counter=1;
TableRow[] tr = new TableRow[5];
for(int j=0;j<5;j++){
tr[j] = new TableRow(ctxt);
for(int i=0;i<5;i++){
tView[i] = new TextView(ctxt);
tView[i].setText(String.valueOf(counter));
tView[i].setTextSize(15);
counter+=1;
tView[i].setWidth(50 * dip);
tView[i].setPadding(20*dip, 0, 0, 0);
tView[i].setTextColor(Color.rgb(100, 200, 200));
Toast.makeText(getApplicationContext(), "tView["+i+"] value " + String.valueOf(tView[i].getText()), Toast.LENGTH_SHORT).show();
tr[j].addView(tView[i], 50, 50);
}
calgrid.addView(tr[j]);
}
}catch(Exception e){
Log.e("MainActivity", "Error in activity", e);
Toast.makeText(getApplicationContext(),e.getClass().getName() + " " + e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
});
謝謝我保持我現有的代碼,它工作後,我刪除了calgrid = new TableLayout(ctxt);從我的代碼中,僅僅因爲這一行代碼不起作用。下次我在實例化一個對象時會小心。我昨天花了整整一天的時間來弄清楚什麼是錯的。你的建議真的很有用。 – Zahary
很高興幫助。如果它回答你的問題,請接受這個答案。 – Craigy