我还是新的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