Android中获取控件宽高的方法集合

  ah_fragmentBottoma = (TextView)findViewById(R.id.ah_fragmentBottoma);
        ah_fragmentBottoma.post(new Runnable() {
            @Override
            public void run() {
                int width = ah_fragmentBottoma.getMeasuredWidth();
                int height = ah_fragmentBottoma.getMeasuredHeight();
                width = ah_fragmentBottoma.getWidth();
                height = ah_fragmentBottoma.getHeight();
                Log.e("test", "post width=" + width);
                Log.e("test", "post height=" + height);
            }
        });

        ViewTreeObserver observer = ah_fragmentBottoma.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ah_fragmentBottoma.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int width = ah_fragmentBottoma.getMeasuredWidth();
                int height = ah_fragmentBottoma.getMeasuredHeight();
                width = ah_fragmentBottoma.getWidth();
                height = ah_fragmentBottoma.getHeight();
                Log.e("test", "addOnGlobalLayoutListener width=" + width);
                Log.e("test", "addOnGlobalLayoutListener height=" + height);
            }
        });

        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = ah_fragmentBottoma.getMeasuredHeight();
                int width = ah_fragmentBottoma.getMeasuredWidth();
                width = ah_fragmentBottoma.getWidth();
                height = ah_fragmentBottoma.getHeight();
                Log.e("test", "addOnPreDrawListener width=" + width);
                Log.e("test", "addOnPreDrawListener height=" + height);
                return true;
            }
        });

      @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            int width = ah_fragmentBottoma.getMeasuredWidth();
            int height = ah_fragmentBottoma.getMeasuredHeight();
            width = ah_fragmentBottoma.getWidth();
            height = ah_fragmentBottoma.getHeight();
            Log.e("test", "onWindowFocusChanged width=" + width);
            Log.e("test", "onWindowFocusChanged height=" + height);
        }
    }

结果:

    addOnGlobalLayoutListener width=132
    addOnGlobalLayoutListener height=88

    addOnPreDrawListener width=132
    addOnPreDrawListener height=88

    post width=132
    post height=88

    onWindowFocusChanged width=132
    onWindowFocusChanged height=88

    addOnPreDrawListener width=132
    addOnPreDrawListener height=88
    addOnPreDrawListener width=132
    addOnPreDrawListener height=88

其中addOnPreDrawListener会调很多次,需要留意。如果有特别的问题再记录。同时后面看源码会分析这个地方。

分析的不错的链接:

Android中为什么在view.post()可以获取到控件的宽高 - Geek的专栏 - CSDN博客

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容