无法显示动态制作的网格布局

时间:2014-04-23 09:30:57

标签: android android-layout grid-layout

这是我创建动态文本视图然后创建网格布局的代码。但网格布局没有显示

TableRow[] tr = new TableRow[arry1.length];
final TextView[] tx = new TextView[arry1.length];
GridLayout tl = (GridLayout) findViewById(R.id.tablelayout1);
tl.setRowCount(arry1.length*2);
tl.setColumnCount(1);
final Button but = new Button(S1.this);
b.setText("hi");

GridLayout[] gl = new GridLayout[arry1.length];

for (i = 0; i < arry1.length; i++) {

final String cat = arry1[i].toString();

tx[i] = new TextView(S1.this);
tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tx[i].setAllCaps(true);
tx[i].setTextSize(15);
tx[i].setText(cat);

gl[i] = new GridLayout(S1.this);
gl[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

gl[i].setVisibility(View.VISIBLE);

tr[i] = new TableRow(S1.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));            

tl.addView(tr[i],new GridLayout.LayoutParams());    
tl.addView(gl[i], new GridLayout.LayoutParams());
tx[i].setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

 category_main = cat;
     gl[i].addView(but);        // Exception        
    }
});

}

当我运行上面的代码时,它会向我显示文本视图,但它不会显示网格布局甚至是按钮。

即使我没有在网格布局中添加按钮,也不会显示网格布局。

当我向动态制作的网格布局添加按钮时抛出异常

请帮帮我。

0 个答案:

没有答案