0

I have a custom layout that includes some TextView and other widgets. The TextViews have an XML attribute android:layout_height="".

The problem is that the layout_height is being ignored and, I think, treated as wrap_content.

I thought that the child views are supposed to handle their own layout_width & layout_height params. Do I need to do something in my custom layout's onMeasure() or is the problem elsewhere?

Thanks.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

0

Apparently the layout_width and layout_height params need to be handled by the container, not the TextView. So, a little piece of code like this seems to do the trick (I'm just showing the height calculation in this example):

for (int i = 0, count = getChildCount();  i < count;  i++)
{
  View child = getChildAt(i);
  ViewGroup.LayoutParams params = child.getLayoutParams();

  int heightSpec;
  if (params.height == ViewGroup.LayoutParams.MATCH_PARENT)
    heightSpec = heightMeasureSpec;
  else if (params.height == ViewGroup.LayoutParams.WRAP_CONTENT)
    heightSpec = MeasureSpec.makeMeasureSpec (Integer.MAX_VALUE, MeasureSpec.UNSPECIFIED);
  else
    heightSpec = MeasureSpec.makeMeasureSpec (params.height, MeasureSpec.EXACTLY);

  child.measure (widthSpec, heightSpec);
}
Peri Hartman
  • 19,314
  • 18
  • 55
  • 101