Measuring a view before rendering it

I need to figure out how big the view will be after attaching it to its parent.

I overridden this method:

onMeasure(int, int); 

but it looks like this method is only called when I actually add my custom view to the container using:

 addView(myView); 

Do you think there is a way to get this information before doing this? Basically, I need to know the size of the actuator before attaching it, rather than attaching the view at all if more specific space is required.

is anyone

+53
android
May 13 '10 at 13:22
source share
6 answers

If you want to get width and height before your activity has added it to your view hierarchy, measured it and laid out, you will have to call measure() on the View yourself before calling getMeasuredWidth() or getMeasuredHeight() .

Since the measured width and height are set after calling the measure (which, in turn, calls onMeasure() ), they return 0 to this point. You will need to put MeasureSpec on measure(..) , which will depend on your needs.

MeasureSpec parameters, which allow children to impose any restrictions themselves, look like

 int widthMeasureSpec = MeasureSpec.makeMeasureSpec(*some width*, MeasureSpec.EXACTLY); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(*some height*, MeasureSpec.EXACTLY); 

The important point . (was changed in the new API) The width specified above can be either explicit px, or ViewGroup.LayoutParams.WRAP_CONTENT or ViewGroup.LayoutParams.FILL_PARENT.

It's also worth noting that when your view is measured in the actual target hierarchy, MeasureSpec, which will be passed to measure() , will be configured based on the ViewGroups layout logic ViewGroups . It can be called more than once if the first vals measures are invalid when viewed in the context of this parent Group view / its own layout restrictions / restrictions of any child sibling view. Without waiting for the viewGroup call measure() , before implementing any logic depending on the measured size, it would be difficult (depending on the situation) to get the final measuredWidth & height from the above solution, but in all cases I used the above method. which was consistent with the goal, but they were relatively simple. If you really need to measure in context, you probably should just do it after onLayout() comes back and explicitly changes any changes, then requestLayout() again.

+84
Dec 06 '11 at 16:53
source share

OnMeasure does not tell you the size of the view. Instead, it asks your custom view to set its size, providing some of the constraints that the parent view fulfills.

This is from the SDK documentation:

The first pair is known as measured width and measured height. These dimensions determine how large the view needs to be within its parent (see Layout for more details.) Measured dimensions can be obtained by calling getMeasuredWidth () and getMeasuredHeight ().

The second pair is simply known as the width and height, and sometimes drawing the width and height of the picture. These sizes determine the actual size of the view on the screen, during drawing and after layout. These values ​​may, but are not necessary, differ from the measured width and height. width and height you can get a call to getWidth () and getHeight ().

After the layout, you can call getWidth () and getHeight () to find the final size of your custom view.

+7
May 13 '10 at 2:00
source share

You should be able to use getMesauredWith () and getMeasuredHeight ()

http://developer.android.com/reference/android/view/View.html#getMeasuredWidth%28%29

0
May 13 '10 at 1:46 p.m.
source share

I have the same problem. I guess the only way is to make it first and then delete it if it matches the size condition. It seems a little strange, but Android seems to know the dimensions after drawing.

0
Aug 18 '11 at 12:50
source share

first call

  view.measure(0, 0); 

then get

view.getMeasuredWidth() or view.getMeasuredHeight()

0
Dec 12 '18 at 8:38
source share

I already answered a very similar question before.

Here are all the ways to do this: https://stackoverflow.com/a/316618/

0
Apr 04 '19 at 13:07 on
source share



All Articles