【问题标题】:In android how do I add buttons into a TableLayout from an array of buttons programmatically?在android中,如何以编程方式将按钮数组中的按钮添加到TableLayout中?
【发布时间】:2012-03-20 05:02:54
【问题描述】:

我是 android 编程的新手,过去 6 周才开始学习它,并且正在为 android 编写一个扫雷游戏,我已经成功地完成了游戏的某些部分而没有太多问题。但是,我必须使用 TableLayout 和 TableRow 以编程方式设计一个网格并在其中插入按钮;所以我写了几行代码来做到这一点,但每当我运行游戏时,我都会收到“确认透视切换”错误。

这是我写的代码 -

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

为什么会出现上述错误?

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

谢谢

【问题讨论】:

    标签: java android


    【解决方案1】:

    如果您想使用 TableLayout,那么您需要构建的内容如下所示(以 4x4 网格为例)

    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 网格)。 在代码中它可能会这样:

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

    【讨论】:

    • 非常感谢,我试过了,效果很好!但是程序有一个不寻常的问题,它运行良好,但是当它假设执行网格活动时,什么都没有发生,让我看到一个空白屏幕!知道为什么会这样吗? [抱歉没有发布完整程序的代码]
    • 你有什么错误或者logcat中有什么相关的吗?
    • 很遗憾没有,logcat 中什么也没有出现
    • 不知道出了什么问题。也许你根本没有设置任何布局/错误的布局(setContentView)。
    • 至于布局,使用如上所示的“layoutVertical”。同时在 xml 中也有一个 LinearLayout !!这样做对吗?
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多