Android获取动态添加视图的位置

时间:2015-07-07 08:56:36

标签: android button dynamic tablelayout layoutparams

onCreate()方法的应用程序中,我需要为每行添加TableLayout一定数量的行和按钮(例如5x5,这意味着我必须为每行添加5行和5个按钮)然后我得到了像方形的东西。这个号码来自intent之前的活动。

然后我需要将按钮的大小更改为最大值,具体取决于TableLayout宽度(我ViewTreeObserver得到的),mach_parent。因此每个按钮的大小为btnWidth = btnHeight = (int) tlWidth / n

之后,我需要将每个按钮添加到数组并获取它的位置,但是当我尝试通过getLocationInWindow()方法获取位置时,我为每个按钮获得相同的位置 - TableLayout的位置。当我调用getLeft()getTop以及getX()getY()时,它始终返回0.即使在onWindowFocusChanged()方法中也会发生这种情况!

这是代码,我写过 在OnCreate

Intent intent = getIntent();
    gridSize = intent.getIntExtra("grid size", 5); //number of buttons I need add
    //Log.d("myLogs", "grid size is " + gridSize);

    corners = new BtnLocation[gridSize][gridSize]; //BtnLocation - object, which holds coordinates of top left corner and bottom right
    cells = new Button[gridSize][gridSize]; //holds each button

    grid = (TableLayout) findViewById(R.id.grid);
    grid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @SuppressLint("NewApi")
        @Override
        public void onGlobalLayout() {
            ViewTreeObserver observer = grid.getViewTreeObserver();
            final int btnSize = (int) grid.getWidth()/gridSize;

            for (int i = 0; i < gridSize; i++) {
                TableRow tr = createRow();
                tr.setGravity(Gravity.CENTER_HORIZONTAL);
                grid.addView(tr);

                for (int j = 0; j < gridSize; j++) {
                    Button btn = createButton();
                    btn.setId(gridSize*i+j);
                    tr.addView(btn);

                    // setting layout params
                    ViewGroup.LayoutParams params = btn.getLayoutParams();
                    params.width = btnSize;
                    params.height = btnSize;
                    btn.setLayoutParams(params);

                    cells[i][j] = btn;

                    int[] location = new int[2];
                    grid.getLocationInWindow(location);
                    Log.d("myLogs", Arrays.toString(location)); 
                    Log.d("myLogs", btn.getLeft() + " " + btn.getTop()); //always write two zeroes
                }
            }

            int[] location = new int[2];
            grid.getLocationInWindow(location);
            Log.d("myLogs", Arrays.toString(location));

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                   observer.removeOnGlobalLayoutListener(this);
               } else {
                   observer.removeGlobalOnLayoutListener(this);
            }
        }
    });
}

onWindowFocusChanged()

for (int i = 0; i < cells.length; i++) {
        for (int j = 0; j < cells.length; j++) {
            int[] location = new int[2];
            grid.getLocationInWindow(location);
            Log.d("myLogs", Arrays.toString(location));
            Log.d("myLogs", cells[i][j].getLeft() + " " + cells[i][j].getTop());
        }
    }

另外,我添加了创建新ButtonTableRow的方法,因为我无法在onGlobalLayout()方法中因为错误而无法使用它们

public TableRow createRow(){
    return new TableRow(this);
}

public Button createButton(){
    return new Button(this);
}

0 个答案:

没有答案