ImageView:getWidth()与getLayoutParams().width不同?

Tob*_*ter 2 android width imageview layoutparams

这是我创建新 ImageView 的源代码:

    ImageView image = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.width = screen_dimens(0.5, "w");
            params.height = screen_dimens(0.5, "w");
            image.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

screen_dimens(0.5, "w")=屏幕宽度的 50%)

但现在当我想获取图像的宽度时,我得到以下结果:
image.getWidth() => 0
image.getLayoutParams().width => 360

现在这两个函数有什么区别?如何设置图像宽度以便该getWidth()方法也返回正确的值?

Gop*_*opi 6

即使您使用 LayoutParams 设置了 ImageView 或任何 View 的宽度和高度,imageView.getWidth() 也始终返回 0,直到在其父级中绘制为止。