Reputation: 5637
This is onMeasure()
on CustomView which extend FrameLayout
. After investigate onMeasure()
, heigh size is always zero. How could I know what is the height size of this CustomView to manipulate child view later.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int viewHeightMode = MeasureSpec.getMode(heightMeasureSpec);
int viewWidthMode = MeasureSpec.getMode(widthMeasureSpec);
int viewHeight = MeasureSpec.getSize(heightMeasureSpec);
int viewWidth = MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Upvotes: 0
Views: 1713
Reputation: 2446
First of all please read this question. It is about measuring of View
.
Main difference about ViewGroup
that you should measure in your code all your childs.
for(int i=0; i<getChildCount(); i++) {
View child = getChildAt(i);
LayoutParams lp = child.getLayoutParams();
int widthMeasureMode = lp.width == LayoutParams.WRAP_CONTENT ? MeasureSpec.AT_MOST : MeasureSpec.EXACTLY,
heightMeasureMode = lp.height == LayoutParams.WRAP_CONTENT ? MeasureSpec.AT_MOST : MeasureSpec.EXACTLY;
int widthMeasure = MeasureSpec.makeMeasureSpec(getWidth() - left, widthMeasureMode),
heightMeasure = MeasureSpec.makeMeasureSpec(getHeight() - top, heightMeasureMode);
child.measure(widthMeasure, heightMeasure);
int childWidth = child.getMeasuredWidth(),
childHeight = child.getMeasuredHeight();
//make something with that
}
That shows how to get size of all childs. May be you want to calculate sum of heights, may be just find maximum value - it is your own goal.
By the way. If your base class is not ViewGroup
, but FrameLayout
for example, measuring childs could be done in onLayout
method. In this case in your onMeasure
method you can do nothing about measuring childs - just take sizes. But it is just a guess - better to check that.
Upvotes: 1