0

我有一个自定义布局,其中包括一些 TextView 和其他小部件。TextViews 有一个 XML 属性 android:layout_height=""。

问题是 layout_height 被忽略了,我认为它被视为 wrap_content。

我认为子视图应该处理自己的 layout_width 和 layout_height 参数。我是否需要在自定义布局的 onMeasure() 中做某事,还是其他地方有问题?

谢谢。

4

1 回答 1

0

显然 layout_width 和 layout_height 参数需要由容器处理,而不是 TextView。所以,像这样的一小段代码似乎可以解决问题(我只是在这个例子中展示了高度计算):

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);
}
于 2015-06-03T22:08:58.973 回答