首页 文章

Android SDK中getWidth / Height()和getMeasuredWidth / Height()有什么区别?

提问于
浏览
72

Android Documentation says视图有两种尺寸,测量尺寸和图纸尺寸 . 测量尺寸是在测量过程中计算的尺寸(onMeasure方法),而图纸尺寸是屏幕上的实际尺寸 . 特别是,文档说:

这些值可能(但不一定)与测量的宽度和高度不同 .

所以,我的问题是:什么可以使绘图尺寸与测量尺寸不同?如果onMeasure(int,int)方法遵循布局要求(作为参数widthMeasureSpec和heightMeasureSpec给出,SDK如何确定视图应该具有不同的绘图大小?

另外,Android Source Code测量宽度/高度的方式/位置用于计算图纸宽度/高度?我试着调查View source code,但我可以't figure out how the measuredWidth/Height is used to compute the final width/height. Maybe it has something to do with the padding, but I'我不确定 .

1 回答

  • 79

    顾名思义,在测量和布局阶段使用了测量的宽度/高度 .

    让我举个例子,

    小部件被要求自己测量,小部件说它想要200px到200px . 这是measuredWidth / height .

    在布局阶段,即在onLayout方法中 . 该方法可以使用其子节点的measuredWidth / height或通过调用视图的布局方法来指定新的宽度/高度 .

    让我们说onLayout方法现在调用childview.layout(0,0,150,150),现在视图的宽度/高度与测量的宽度/高度不同 .

    我建议不要在onLayout方法之外使用measuredWidth / height .

    总结一下 .

    • onMeasure - >设置measuredWidth / measuredHeight

    • onLayout - >设置小部件的宽度/高度 .

    additionallly
    public void View.layout(int l, int t, int r, int b)
    似乎是位置和大小分配发生的地方 .

相关问题