Android:以编程方式添加多个按钮错误

Android: Adding multiple buttons programmatically bug

所以我想在相对布局上添加随机数量的按钮 (9,16,25) 以形成一种网格。我想在 activity 的开头以编程方式添加按钮。 例如:

1 2 3
4 5 6
7 8 9

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

我创建了一个名为 BOARD 的对象,其中包含一组 9 个 TILES,例如必须排列在 3x3 网格中

这是我添加按钮的方式:

 buttons = new ArrayList<>();
        for (int row = 0; row <  board.getNoOfRows(); row ++){
            for (int column = 0; column < board.getNoOfColumns() ; column ++){
                // first button
                if(row == 0 && column == 0){
                    buttons.add(addTile(board.getTile(currentButtonIndex) , null, null));

                }
                // next buttons on the first row
                else if(row == 0) {
                    buttons.add(addTile(board.getTile(currentButtonIndex) , buttons.get(currentButtonIndex - 1), null));

                }

                // first buttons on subsequent rows
                else if (row > 0 && column == 0){
                    buttons.add(addTile(board.getTile(currentButtonIndex) , null, buttons.get(currentButtonIndex - board.getNoOfColumns())));

                }
                // the rest of the buttons
                else if (row > 0 && column > 0){
                    buttons.add(addTile(board.getTile(currentButtonIndex) , buttons.get(currentButtonIndex - 1), buttons.get(currentButtonIndex - board.getNoOfColumns())));
                }
                // add the buttons on the view
                rlBoardView.addView(buttons.get(currentButtonIndex));
                currentButtonIndex ++;
            }
        }
        rlBoardView.setGravity(Gravity.CENTER);

这是 addTile 方法:

private Button addTile(Tile tileParam, Button leftButton, Button aboveButton){
        Button tile = new Button(this);
        tile.setId(tileParam.getIndex());
        // setup buttons params
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        tile.setTextSize(15);
        // if button exists on the left, add it to it's right
        if(leftButton != null){
            params.addRule(RelativeLayout.RIGHT_OF, leftButton.getId());
        }
        // if button exists above add it below it
        if(aboveButton != null){
            params.addRule(RelativeLayout.BELOW, aboveButton.getId());
        }else{

        }
        tile.setLayoutParams(params);
        tile.setOnClickListener(this);
        tile.setText(String.valueOf(tileParam.getNumber()));

        return tile;
    }

问题是生成的网格显示如下:

2x2 grid   3x3 grid         4x4 grid
2          3 2              4  2  3
  3        6 4 5            8  5  6  7
             7 8           12  9 10 11 
                              13 14 15

编号从0开始

我不知道我做错了什么。有些按钮位于正确的位置,而另一些则完全关闭。按钮 0 也没有出现。

第一个按钮没有任何规则。您可能应该添加:ALIGN_PARENT_LEFT 和 ALIGN_PARENT_TOP。 所以:

if (leftButton == null && aboveButton == null) {
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}

此外,对于下一行中的第一个按钮,您应该添加 ALIGN_PARENT_LEFT。

使用自定义 gridView 并根据需要进行更改。看看 this sample。很好的开始。

我已经找到问题所在。是在这一行:

tile.setId(tileParam.getIndex());

虽然每个图块都有一个唯一的索引,但显然存在某种冲突。我将其替换为:

tile.setId(View.generateViewId());

现在一切正常。