Android SDK 中的 getWidth/Height() 和 getMeasuredWidth/Height() 有什么区别? [英] What is the difference between getWidth/Height() and getMeasuredWidth/Height() in Android SDK?

查看:29
本文介绍了Android SDK 中的 getWidth/Height() 和 getMeasuredWidth/Height() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 文档说有两种尺寸一个视图,测量尺寸绘图尺寸.测量维度是在测量传递(onMeasure 方法),而绘图尺寸是屏幕上的实际尺寸.特别是,文档说:

The Android Documentation says that there is two sizes for a view, the measured dimensions and the drawing dimensions. The measured dimension is the one computed in the measure pass (the onMeasure method), while the drawing dimensions are the actual size on screen. Particularly, the documentation says that:

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

These values may, but do not have to, be different from the measured width and height.

所以,我的问题是:什么可以使图纸尺寸与测量尺寸不同?如果 onMeasure(int,int)方法尊重布局要求(给定参数widthMeasureSpecheightMeasureSpec,SDK如何决定视图应该有不同的绘图尺寸?

So, my question is: what could make the drawing dimension be different of the measured dimension? If the onMeasure(int,int) method respects the layout requirements (given as the parameters widthMeasureSpec and heightMeasureSpec, how could the SDK decides that the view should have a different drawing size?

此外,Android 源代码中的 how/where 测量的宽度/高度用于计算绘图宽度/高度?我试图查看 查看源代码,但我无法弄清楚如何使用测量的宽度/高度来计算最终的宽度/高度.也许它与填充有关,但我不确定.

Additionally, how/where in the Android Source Code the measured width/height is used to compute the drawing width/height? I tryed to look into the View source code, but I can'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'm not sure.

推荐答案

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

As the name suggests the measuredWidth/height is used during measuring and layoutting phase.

举个例子,

一个小部件被要求测量自己,小部件说它想要 200 像素 x 200 像素.这是测量的宽度/高度.

A widget is asked to measure itself, The widget says that it wants to be 200px by 200px. This is measuredWidth/height.

在布局阶段,即在 onLayout 方法中.该方法可以使用其子项的测量宽度/高度或通过调用视图的布局方法分配新的宽度/高度.

During the layout phase, i.e. in onLayout method. The method can use the measuredWidth/height of its children or assign a new width/height by calling layout method of the view.

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

lets say the onLayout method calls childview.layout(0,0,150,150) now the width/height of the view is different than the measured width/height.

我建议不要在 onLayout 方法之外使用测量宽度/高度.

I would suggest not to use the measuredWidth/height outside onLayout method.

总结一下.

  1. onMeasure -> 设置测量宽度/测量高度
  2. onLayout -> 设置小部件的宽度/高度.

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

additionallly
public void View.layout(int l, int t, int r, int b)
seems to be place where the assignment of position and size happens.

这篇关于Android SDK 中的 getWidth/Height() 和 getMeasuredWidth/Height() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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