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

作者: MonkeyLei | 来源:发表于2019-08-14 09:35 被阅读6次
      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博客

    相关文章

      网友评论

        本文标题:Android中获取控件宽高的方法集合

        本文链接:https://www.haomeiwen.com/subject/llmwjctx.html