2012-03-19 52 views
1

刚刚接触android编程,刚开始学习它过去6周,并正在为Android编写扫雷游戏,以及我已经设法做了一些游戏的一部分,没有太多问题。但是,我必须使用TableLayout和TableRow以编程方式设计网格,并在其中插入按钮;所以我写了几行代码来做到这一点,但每当我运行游戏时,我都会收到“确认透视图切换”错误。在android中,我该如何从编程的按钮数组中添加按钮到TableLayout中?

下面是我写的代码 -

` public class Game extends Activity implements OnClickListener { 

     Button[][] btn = new Button[6][6]; 
     public void onCreate(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.gamegrid); 

      int i, j; 

      LinearLayout layoutVertical = (LinearLayout) findViewById(R.layout.gamegrid); 
      //create a new TableLayout 
      TableLayout table = null; 

      table.setStretchAllColumns(true); 
      table.setShrinkAllColumns(true); 

      LayoutParams param = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 

      for(i = 0; i <6; i++){ 
       table = new TableLayout(this); 
       table.setWeightSum(5); 
       layoutVertical.addView(table, param); 
       for(j=0; j<7; j++){ 
        btn[i][j] = new Button(this); 
        table.addView(btn[i][j], param);  
        btn[i][j].setOnClickListener(this); 
        } 
      } return; 
     } 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

     } 

    } ` 

我想我的问题是与以下几行 -

`for(i = 0; i <6; i++){ 
table = new TableLayout(this); 
    table.setWeightSum(5); 
    layoutVertical.addView(table, param); 
    for(j=0; j<7; j++){ 
     btn[i][j] = new Button(this); 
     table.addView(btn[i][j], param); 
     btn[i][j].setOnClickListener(this); 
     } 
    }` 

它想创建按钮,然后将它们存储在按钮的数组然后将按钮插入TableLayout!

为什么我会收到上述错误?

你能帮我指出做错了什么吗?因为我没有任何错误显示。

感谢

回答

1

如果你想使用一个TableLayout,那么你需要构造看起来像这样(使用4x4的方格,例如),它包含4个TableRows

TableLayout 
    TableRow 
     Button 
     Button 
     Button 
     Button 
    TableRow 
     Button 
     Button 
     Button 
     Button 
    TableRow 
     Button 
     Button 
     Button 
     Button 
    TableRow 
     Button 
     Button 
     Button 
     Button 

1 TableLayout什么,每行包含4个按键(4×4格)。 在代码中,它可能会这样:

Button[][] buttonArray = new Button[4][4]; 
TableLayout table = new TableLayout(context); 
for (int row = 0; row < 4; row++) { 
    TableRow currentRow = new TableRow(context); 
    for (int button = 0; button < 4; button++) { 
     Button currentButton = new Button(context); 
     // you could initialize them here 
     currentButton.setOnClickListener(listener); 
     // you can store them 
     buttonArray[row][button] = currentButton; 
     // and you have to add them to the TableRow 
     currentRow.addView(currentButton); 
    } 
    // a new row has been constructed -> add to table 
    table.addView(currentRow); 
} 
// and finally takes that new table and add it to your layout. 
layoutVertical.addView(table); 
+0

非常感谢我尝试过,它工作正常!但是,如果程序有一个不寻常的问题,它运行良好,但是当它想要执行网格活动时,没有任何反应让我留下空白屏幕!任何想法为什么这可能是? [抱歉没有发布完整程序的代码] – Ange 2012-04-17 20:04:26

+0

你有任何错误或有任何相关的logcat? – zapl 2012-04-17 20:38:29

+0

不幸的是,没有什么出现在logcat – Ange 2012-04-17 20:55:24

0

您要添加的,而不是有一个有许多行/列的多个表。流程应该是这样的:

final LinearLayout layout = (LinearLayout)activity.findViewById(com.myapp.tableContainer); 
final TableLayout table = new TableLayout(context); 
layout.add(table, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

final TableRow row = new TableRow(context); 

TextView textView = new TextView(context); 
textView.setText("0"); 
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1)); 
row.addView(textView, 0); 

textView = new TextView(context); 
textView.setText("1"); 
textView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1)); 
row.addView(textView, 1); 

table.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, 
       LayoutParams.MATCH_PARENT, 1)); 

并继续以相同的方式添加更多的行。

相关问题