get layout height and width at run time android

前端 未结 6 1252
死守一世寂寞
死守一世寂寞 2020-11-29 05:50

How can I get width and height of a linear layout which is defined in xml as fill_parent both in height and width? I have tried onmeasure method but I dont know why it is no

6条回答
  •  离开以前
    2020-11-29 06:31

    The width and height values are set after the layout has been created, when elements have been placed they then get measured. On the first call to onSizeChanged the parms will be 0 so if you use that check for it.

    Little more detail here https://groups.google.com/forum/?fromgroups=#!topic/android-developers/nNEp6xBnPiw

    and here http://developer.android.com/reference/android/view/View.html#Layout

    Here is how to use onLayout:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = someView.getWidth();
        int height = someView.getHeight();
    }
    

提交回复
热议问题