将新的 TextView 添加到 ConstraintLayout 在 Create 时不起作用,但之后会起作用

问题描述 投票:0回答:1

我正在制作一个彩色 TextView 的动态网格来制作图块,因为它是动态的,所以我希望 gridSize 能够在运行时更改,我需要以编程方式进行。

问题是,当我在 onCreate 方法中生成网格单元格时,它什么也不做[至少这是我猜测的,因为我没有看到任何东西],但是当我从它所做的活动是它的工作[我完美地看到了网格]

这是onCreate方法,创建网格的函数是

generateGrid()
【我把参数传给了View v,这样我就可以测试它是不起作用还是只在onCreate上不起作用】:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(Utils.currentTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duek48);

        generateGrid(new View(this)); //this doesn't work for some reason
    }

这是我创建的用于生成网格的方法:

    public void generateGrid(View v) {
        TextView previousCell = null; //to attach the current cell to the previous one
        for (int i = 0; i < gridSize * gridSize; i++) {
            ConstraintLayout grid = findViewById(R.id.grid);
            int gapSize = 16; //this needs improvement: it has to be automatic, proportionally inverse to the grid size
            int cellSize = (grid.getHeight() - (gapSize * (gridSize  + 1))) / gridSize;
            TextView gridBackgroundCell = new TextView(this);
            gridBackgroundCell.setId(View.generateViewId());
            gridBackgroundCell.setText("");
            gridBackgroundCell.setBackgroundTintList(ColorStateList.valueOf(Color.rgb(170, 170, 170)));
            gridBackgroundCell.setBackground(getResources().getDrawable(R.drawable.rounded_view));
            grid.addView(gridBackgroundCell);
            //add constraints
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(cellSize, cellSize);
            if (previousCell == null) {
                layoutParams.topMargin = gapSize;
                layoutParams.topToTop = R.id.grid;
                layoutParams.startToStart = R.id.grid;
            } else {
                if (i % gridSize == 0) {
                    layoutParams.topMargin = gapSize;
                    layoutParams.topToBottom = previousCell.getId();
                    layoutParams.startToStart = R.id.grid;
                } else {
                    layoutParams.topMargin = 0;
                    layoutParams.topToTop = previousCell.getId();
                    layoutParams.startToEnd = previousCell.getId();
                }
            }
            layoutParams.leftMargin = gapSize;
            gridBackgroundCell.setLayoutParams(layoutParams);

            grid.updateViewLayout(gridBackgroundCell, layoutParams);

            previousCell = gridBackgroundCell;
        }
    }

我意识到,当我删除所有 LayoutParams 部分时 [我将引用它只是为了清楚起见],即使在 onCreate 方法中,也会显示文本,因此它可以完美工作,但是当我将其放回去时,它不会:

    ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(cellSize, cellSize);
    if (previousCell == null) {
        layoutParams.topMargin = gapSize;
        layoutParams.topToTop = R.id.grid;
        layoutParams.startToStart = R.id.grid;
    } else {
        if (i % gridSize == 0) {
            layoutParams.topMargin = gapSize;
            layoutParams.topToBottom = previousCell.getId();
            layoutParams.startToStart = R.id.grid;
        } else {
            layoutParams.topMargin = 0;
            layoutParams.topToTop = previousCell.getId();
            layoutParams.startToEnd = previousCell.getId();
        }
    }
    layoutParams.leftMargin = gapSize;
    gridBackgroundCell.setLayoutParams(layoutParams);
    grid.updateViewLayout(gridBackgroundCell, layoutParams);

就好像在 onCreate 函数中设置 LayoutParams 时遇到问题一样。 如果有人有任何建议请帮忙:)

android android-constraintlayout oncreate layoutparams
1个回答
0
投票

好吧,问题是 int

cellSize
是负数,因为具有 LAYOUT 高度而不是真实高度的视图的 getHeight() 方法似乎返回负值 -> 我正在设置每个视图的布局高度查看负数 我是慢热型。简单

© www.soinside.com 2019 - 2024. All rights reserved.