Android 在渲染之前测量视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2827079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
measuring a view before rendering it
提问by nourdine
I need to find out how big a view will be after attaching it to its parent.
我需要找出将视图附加到其父视图后的大小。
I have overridden this method:
我已经覆盖了这个方法:
onMeasure(int, int);
but it looks like this method is invoked only when I actually add my custom view to it's container using:
但看起来只有当我实际使用以下方法将自定义视图添加到它的容器时才会调用此方法:
addView(myView);
Do you think there is a way to get this information before rendering the view itself? Basically I need to know the actuall size before attaching it and not attach the view at all if it would take more certain amount of space.
你认为有没有办法在渲染视图本身之前获取这些信息?基本上,我需要在附加之前知道实际尺寸,如果需要更多空间,则根本不附加视图。
anybody?
有人吗?
回答by Dori
If you want to get the width and height before your activity has added it to its view hierarchy, measured and layed it out you will have to call measure()
on the View
yourself before calling getMeasuredWidth()
or getMeasuredHeight()
.
如果你希望得到您的活动之前的宽度和高度已经把它添加到它的视图层次,测量和布置它,你将不得不调用measure()
在View
调用之前自己getMeasuredWidth()
或getMeasuredHeight()
。
As the measured width and height are set after measure has been called (which in turn calls onMeasure()
) they will return 0 before this point. You will have to supply MeasureSpec
s to measure(..)
that will vary depending on your needs.
由于测量的宽度和高度是在调用 measure 之后设置的(后者又调用onMeasure()
),因此它们将在此点之前返回 0。您将不得不根据您的需要提供MeasureSpec
s measure(..)
。
The MeasureSpecs that allow the children to impose any constraints themselves look like
允许孩子们自己施加任何约束的 MeasureSpecs 看起来像
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(*some width*, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(*some height*, MeasureSpec.EXACTLY);
Important point. (has been changed in new API)The width passed in above can either be explicit px or ViewGroup.LayoutParams.WRAP_CONTENT
or ViewGroup.LayoutParams.FILL_PARENT.
重要的一点。(已在新 API 中更改)上面传入的宽度可以是显式 pxViewGroup.LayoutParams.WRAP_CONTENT
或ViewGroup.LayoutParams.FILL_PARENT.
Its also worth noting when your view is measured in the actual destination hierarchy the MeasureSpec that will be passed to measure()
will be configured based upon the containing ViewGroups
layout logic. It may be called more than once if the first measure vals are not valid when considered in the context of this parent viewGroup / its own layout constraits / the constraints of any sibling child-views. Without waiting for the viewGroup
to call measure()
before implementing any logic dependent on the measured size it would be hard (depending on the situation) to get the final measuredWidth & height
from the above solution, but in all the instances i have used the above technique it has fit the purpose, but they have been relatively simple. If you really do need to measure in context you should probably just do it after onLayout()
has returned and explicitly set any changes then requestLayout()
again.
还值得注意的是,当您的视图在实际目标层次结构中进行测量时,将根据包含的布局逻辑measure()
配置将传递到的 MeasureSpec 。如果在此父视图组/其自己的布局约束/任何同级子视图的约束的上下文中考虑第一个度量值无效,则可能会多次调用它。在实现依赖于测量大小的任何逻辑之前不等待调用,将很难(取决于情况)获得最终结果ViewGroups
viewGroup
measure()
measuredWidth & height
从上述解决方案,但在所有情况下,我都使用了上述技术,它符合目的,但它们相对简单。如果您确实需要在上下文中进行测量,您可能应该在onLayout()
返回后进行测量,然后requestLayout()
再次明确设置任何更改。
回答by Prashast
OnMeasure does not tell you the size of the View. Instead, it asks your custom View to set its size by providing some constraints enforced by the parent View.
OnMeasure 不会告诉您视图的大小。相反,它通过提供父视图强制执行的一些约束来要求您的自定义视图设置其大小。
This is from the SDK documentation:
这是来自 SDK 文档:
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see Layout for more details.) The measured dimensions can be obtained by calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling getWidth() and getHeight().
第一对称为测量宽度和测量高度。这些尺寸定义了一个视图在其父级中想要有多大(有关更多详细信息,请参阅布局。)可以通过调用 getMeasuredWidth() 和 getMeasuredHeight() 获得测量尺寸。
第二对简单地称为宽度和高度,或有时绘制宽度和绘制高度。这些尺寸定义了屏幕上、绘制时和布局后视图的实际大小。这些值可能(但不一定)与测量的宽度和高度不同。可以通过调用 getWidth() 和 getHeight() 获取宽度和高度。
After layout you can call getWidth() and getHeight() to find the final size of your custom View.
布局后,您可以调用 getWidth() 和 getHeight() 来查找自定义视图的最终大小。
回答by Airforce-1
first call
第一个电话
view.measure(0, 0);
then get
然后得到
view.getMeasuredWidth()
or view.getMeasuredHeight()
view.getMeasuredWidth()
或者 view.getMeasuredHeight()
回答by Dan2552
I'm having the same problem. I guess the only way is to render it first, then remove it afterif it meets the size condition. Seems a bit of an odd way, but Android only seems to know the dimensions after drawing.
我有同样的问题。我想唯一的方法是先渲染它,然后在满足大小条件后将其删除。看起来有点奇怪,但Android似乎只知道绘制后的尺寸。
回答by Dan2552
You should be able to use getMesauredWith() and getMeasuredHeight()
您应该能够使用 getMesauredWith() 和 getMeasuredHeight()
http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29
http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29