2022-10-08 android app oncreate()中获取到控件的高度和宽度值为0解决办法,获取控件高宽的几个方法。

一、在activity 的oncreate()方法中获取声明的空间的高度或者宽度,调用view.getHeight()或者view.getWidth()获取的竟然为0。因为在oncreate的时候,实际上控件只是声明了,并没有绘制完成。

二、方法一

view.post(new Runnable() {
     @Override
     public void run() {
         int height = view.getHeight() ;
         int width = view.getWidth();
      }
});

三、方法二

        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int height = textView.getMeasuredHeight();
                int width = textView.getMeasuredWidth();
                Log.i("visitView addOnGlobalLayoutListener", "height:"+height+" width:"+width);
            }
        });

四、方法三

        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                textView.getViewTreeObserver().removeOnPreDrawListener(this);
                int height = textView.getMeasuredHeight();
                int width = textView.getMeasuredWidth();
                Log.i("visitView addOnPreDrawListener", "height:"+height+" width:"+width);
                return true;
            }
        });

五、方法四

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //获取宽度image.getWidth();
            //获取高度image.getHeight();
            int height = textView.getWidth();
            int width = textView.getHeight();
            Log.i("visitView onWindowFocusChanged", "height:" + height + " width:" + width);
        }
    }

六、实例测试

6.1 布局文件

 6.2 java 代码

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textView = (TextView) findViewById(R.id.tv);

        int height = textView.getMeasuredHeight();
        int width = textView.getMeasuredWidth();
        Log.i("visitView", "height:"+height+" width:"+width);

        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                textView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int height = textView.getMeasuredHeight();
                int width = textView.getMeasuredWidth();
                Log.i("visitView addOnGlobalLayoutListener", "height:"+height+" width:"+width);
            }
        });

        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                textView.getViewTreeObserver().removeOnPreDrawListener(this);
                int height = textView.getMeasuredHeight();
                int width = textView.getMeasuredWidth();
                Log.i("visitView addOnPreDrawListener", "height:"+height+" width:"+width);
                return true;
            }
        });

        textView.post(new Runnable() {
            @Override
            public void run() {
                int height = textView.getHeight() ;
                int width = textView.getWidth();
                Log.i("visitView post", "height:"+height+" width:"+width);
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            //获取宽度image.getWidth();
            //获取高度image.getHeight();
            int height = textView.getWidth();
            int width = textView.getHeight();
            Log.i("visitView onWindowFocusChanged", "height:" + height + " width:" + width);
        }
    }

 6.3 真机测试结果

 六、参考文章

(664条消息) android 开发 在oncreate()中获取到控件的高度和宽度值为0解决办法_kinghore的博客-CSDN博客

(664条消息) Android 在Oncreate中获取控件的高度_huangxy10的博客-CSDN博客

(664条消息) Android 在onCreate()方法中获取控件宽高值为0解决方案_蹲街式等待的博客-CSDN博客_安卓获取控件宽高为0

(664条消息) Android Activity加载完成事件_Hunter_Tang的博客-CSDN博客_android载入事件

猜你喜欢

转载自blog.csdn.net/qq_37858386/article/details/127213785