How to get an Android widget's size after layout is calculated?

前端 未结 2 1673
庸人自扰
庸人自扰 2020-12-03 21:42

I have a layout which specifies sizes of widgets in relative dimension, for example:


     

        
2条回答
  •  隐瞒了意图╮
    2020-12-03 22:17

    The View object provides method called onSizeChanged(int w, int h, int oldw, int oldh). It is called when the layout changes the size of one of its components. If you override this method then the the code in your version of this method will know the size of itself as shown in the following snippet.

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        bounds = new Rect();
        this.getDrawingRect(bounds);
        // bounds will now contain none zero values
    }
    

提交回复
热议问题