【问题标题】:Android: Adding multiple buttons programmatically bugAndroid:以编程方式添加多个按钮错误
【发布时间】:2017-02-03 13:26:25
【问题描述】:

所以我想在相对布局上添加随机数量的按钮(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 也没有出现。

【问题讨论】:

    标签: android button android-relativelayout


    【解决方案1】:

    您对第一个按钮没有任何规则。您可能应该添加: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。

    【讨论】:

    • 添加额外的规则没有任何区别。它具有相同的输出。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      tile.setId(tileParam.getIndex());
      

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

      tile.setId(View.generateViewId());
      

      现在一切正常。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-06-20
        相关资源
        最近更新 更多