如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0 [英] If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0

查看:22
本文介绍了如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0.我认为目前还没有绘制该视图.

If I call getMeasuredWidth() or getWidth() for layout in onResume they returns 0. I think that view it's not drawn yet in this moment.

此外,我认为我需要将 getMeasuredWidth() 或 getWidth() 放在绘制布局且视图测量已知后调用的回调方法中.我应该使用什么 android 回调方法?

Also I think that I need to put getMeasuredWidth() or getWidth() in callback method called after layout is drawn and view measurements are known. What android callback method should I use?

推荐答案

你不能使用width/height/getMeasuredWidth/View 上 >getMeasuredHeight(通常来自 onCreate/onResume).

You cannot use the width/height/getMeasuredWidth/getMeasuredHeight on a View before the system renders it (typically from onCreate/onResume).

对此的简单解决方案是将 Runnable 发布到布局.runnable 将在 View 布局后执行.

Simple solution for this is to post a Runnable to the layout. The runnable will be executed after the View has been laid out.

BoxesLayout = (RelativeLayout) findViewById(R.id.BoxesLinearLayout);
BoxesLayout.post(new Runnable() {
    @Override
    public void run() {
        int w = BoxesLayout.getMeasuredWidth();
        int h = BoxesLayout.getMeasuredHeight();

        ...
    }
});

这篇关于如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们将返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆