在Android中如何添加按钮到TableLayout从按钮编程的数组? [英] In android how do I add buttons into a TableLayout from an array of buttons programmatically?

查看:151
本文介绍了在Android中如何添加按钮到TableLayout从按钮编程的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是新到Android编程,刚开始学习它在过去6周,我写一个扫雷游戏为Android,嗯,我已经成功地做​​游戏的某些部分没有太多的问题。但是,我必须设计编程方式使用TableLayout和的TableRow和插入按钮在其中一格;所以我写了code的几行这样做,但每当我运行游戏,我得到确认透视图切换的错误。

Am new to android programming, just started learning it the past 6 weeks and am writing a minesweeper game for android, well i've managed to do some part of the game without much issues. However, I've got to design a grid programmatically using TableLayout and TableRow and insert buttons in them; so I've written few lines of code to do that but whenever I run the game i get "Confirm Perspective Switch" error.

下面是codeS我已经写了 -

Here are the codes I've written -

` 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

        }

    } `

我觉得我的问题是与以下行 -

I think my problem is with the following lines -

`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!

It's suppose to create buttons then store them in an array of buttons then insert the buttons in the TableLayout!

为什么我收到上述错误?

And why am I getting the above error?

能否请你帮我指出什么做错了什么?由于我没有任何错误显示。

Could you please help me point out what am doing wrong? As I do not have any errors showing.

感谢

推荐答案

如果你想用一个TableLayout,那么你需要构造看起来像这样(使用4x4网格为例)是什么

If you want to use a TableLayout then what you need to construct looks like this (using a 4x4 grid as example)

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

1 TableLayout包含4 TableRows,每行包含4个按键(4x4网格)。
在code,它希望,所以也许:

1 TableLayout that contains 4 TableRows and each row contains 4 Buttons (a 4x4 grid). In code it would like so maybe:

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);

这篇关于在Android中如何添加按钮到TableLayout从按钮编程的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆