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

发布于 2024-12-23 07:27:15 字数 990 浏览 3 评论 0 原文

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

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

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

此外,如何/在哪里在 Android 源代码 测量的宽度/高度用于计算绘图宽度/高度?我试图查看 查看源代码,但我不知道了解如何使用测量的宽度/高度来计算最终的宽度/高度可能与填充有关,但我不确定。

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.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2024-12-30 07:27:15

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

让我举个例子,

一个小部件被要求测量自己,小部件说它想要成为 200px x 200px。这是测量的宽度/高度。

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

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

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

总结一下。

  1. 测量 -> 在Layout上设置measuredWidth/measuredHeight
  2. ->设置小部件的宽度/高度。

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

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

Let me give an example,

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

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.

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.

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

to summarize .

  1. onMeasure -> sets up measuredWidth/measuredHeight
  2. onLayout -> sets up the width/height of the widget.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文