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

时间:2017-02-03 13:26:26

标签: android button relativelayout

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

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也不会出现。

3 个答案:

答案 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。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我设法找到了问题。就在这条线上:

tile.setId(tileParam.getIndex());

虽然每个瓷砖都有一个独特的索引,但显然存在某种冲突。我把它改为:

tile.setId(View.generateViewId());

现在一切都按预期工作了。

相关问题