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

问题描述

借助 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:

这些值可以,但不必,是从所测量的宽度和高度不同。

所以,我的问题是:什么可以使图纸尺寸是不同的测量尺度呢?如果onMeasure(int,int)法尊重布局的要求(给出的参数的 widthMeasureSpec heightMeasureSpec 的,怎么可能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源$ C ​​$ C 测量宽度/高度是用来计算绘制宽/高?我trye​​d寻找到的View来源$ C ​​$ C ,但我无法弄清楚如何是measuredWidth /身高来计算最终的宽度/高度。也许它是与填充,但我不知道。

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.

推荐答案

正如其名称所暗示的测量和layoutting阶段期间是measuredWidth /高度被使用。

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

让我举一个例子,

一个小部件被要求来衡量自己,窗口小部件表示,它希望成为200像素由200像素。这是measuredWidth /高度。

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

在布局阶段,即onLayout方法。该方法可以使用​​其子项的measuredWidth /高度或致电布局视图的方法分配一个新的宽度/高度。

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.

我会建议不要使用是measuredWidth /高onLayout方法之外。

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

来概括。

  1. onMeasure - >设置是measuredWidth / measuredHeight可以
  2. onLayout - >设置widget的宽度/高度

additionallly
公共无效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 /高()和getMeasuredWidth /身高()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:23